CodeIgniter Forums
How to avoid errors in view when I am using variable for the php session - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to avoid errors in view when I am using variable for the php session (/showthread.php?tid=26269)

Pages: 1 2


How to avoid errors in view when I am using variable for the php session - El Forum - 01-10-2010

[eluser]shinokada[/eluser]
I am using php session.

In a view when I use the following code, I get the error notice.

It is not displayed, but when I see the source code, it's there.

In a view:

Code:
$totalprice = $_SESSION['totalprice'];

Error

Code:
<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined index:  totalprice</p>
<p>Filename: controllers/mycontroller.php</p>
<p>Line Number: 326</p>

If I put it a controller, I get another error.

In a controller:
Code:
$data['totalprice']= $_SESSION['totalprice'];

Error

Code:
<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined index:  totalprice</p>
<p>Filename: controllers/mycontroller.php</p>
<p>Line Number: 326</p>

The second case is worse since the doc type and other header section is gone.

Can anyone tell me how to do it?

(Please don't ask me or why I am not using ci session class.)


How to avoid errors in view when I am using variable for the php session - El Forum - 01-10-2010

[eluser]Sbioko[/eluser]
You see this error, because there are no totalprice item in your session. Use this to see your session's items:
Code:
print_r($_SESSION);



How to avoid errors in view when I am using variable for the php session - El Forum - 01-10-2010

[eluser]Yorick Peterse[/eluser]
As said by demedes, you get that error since the item doesn't exist. Use the following code to check whether the item exists or not. You should also note that plain PHP sessions don't work with CodeIgniter by default.

Code:
&lt;?php
if(isset($_SESSION['some_key'])) {
    echo $_SESSION['some_key'];
}
else
{
    $_SESSION['some_key'] = 'some value';
}
?&gt;



How to avoid errors in view when I am using variable for the php session - El Forum - 01-10-2010

[eluser]shinokada[/eluser]
It is there.

If I do

Code:
print_r ($_SESSION['totalprice']);

It print out 418.

And if I do

Code:
print_r ($_SESSION);

It will print out all other information.


How to avoid errors in view when I am using variable for the php session - El Forum - 01-10-2010

[eluser]Sbioko[/eluser]
Code:
print_r($_SESSION);
This code outputs all the session's items.

Quote:(Please don’t ask me or why I am not using ci session class.)
Sorry but, why you don't use the native CI session library? :-)


How to avoid errors in view when I am using variable for the php session - El Forum - 01-10-2010

[eluser]aidehua[/eluser]
Can you show us the full print-out result when you run
Code:
print_r ($_SESSION);
?

(Preferably copied from the source so that it's nicely laid out with line-breaks...)


How to avoid errors in view when I am using variable for the php session - El Forum - 01-10-2010

[eluser]shinokada[/eluser]
The reason I am not using CI session.

1. I am using code from Codeigniter Professional which uses PHP session.
2. Please read this thread http://ellislab.com/forums/viewthread/130577/


How to avoid errors in view when I am using variable for the php session - El Forum - 01-10-2010

[eluser]shinokada[/eluser]
@aidehua

It does not show in the source. But it is on the page.

print_r ($_SESSION); outputs the followings.

Code:
Array (
[totalprice] => 954
[cart] => Array (
      [115] => Array (
      [name] => MÅNESKINN
      [price] => 268.00
      [count] => 1 )
[80] => Array (
      [name] => DELFINLEK  
      [price] => 268.00
      [count] => 1 )
[68] => Array (
      [name] => OPPDAGELSEN
      [price] => 418.00
      [count] => 1 ) )
[shipping] => 65 )



How to avoid errors in view when I am using variable for the php session - El Forum - 01-10-2010

[eluser]aidehua[/eluser]
Thanks sinokada.

[Yes, sorry, when I said "source" I meant the HTML source (View...Source) rather than the rendered browser view, if you know what I mean.]

Not sure where the problem lies then.

You said you're getting the error when you run

Code:
$totalprice = $_SESSION['totalprice'];

And yet you don't get the error on running:

Code:
print_r ($_SESSION['totalprice']

And if you just

Code:
echo  $_SESSION['shipping'];

?

And how about

Code:
$shipping = $_SESSION['shipping'];

Does that work OK?


How to avoid errors in view when I am using variable for the php session - El Forum - 01-10-2010

[eluser]shinokada[/eluser]
As I said the error does not come out on the web site. But if I see the source it is there.

I may be ignore the error.