CodeIgniter Forums
Storing "0" Zero in a session as a variable - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter Archived (from EllisLab) (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Storing "0" Zero in a session as a variable (/thread-43606.html)



Storing "0" Zero in a session as a variable - El Forum - 07-18-2011

[eluser]nevsie[/eluser]
I am pretty sure i know why this is occurring - but just wondered if there are any gotchas around this?

I have a form that submits a number of scored answers values 0-9. These values are then held in a session for a couple of screen where further questions are asked, until finally something is done with them at the end...

All numbers store and work fine... except "0". This gets submitted to the form, but when added in the session, and viewing the session data nothing is there. Now i assume this is because "0" is nothing, and is seen as null or false... But i need to see that "0" for scoring purposes where i add up the values.

So is there some stupid gotcha that i am not thinking about here, or is CI filtering it out on my behalf i a need to change a setting, or any obvious ways that this should be handled that i as a non programmer should really know about?

Thanks, N

EDIT
I should add that i need the "0" for referencing an array key - hence i need the "0".


Storing "0" Zero in a session as a variable - El Forum - 07-18-2011

[eluser]rogierb[/eluser]
Hi,

Post the code where you save the '0' value in the session and validation rules if any.
Without code itÅ› rather hard to see why or how.


Storing "0" Zero in a session as a variable - El Forum - 07-18-2011

[eluser]nevsie[/eluser]
Hi Roger,
Thanks for your reply - i must admit i was assuming that CI was filtering the values before putting them into the session. However, after the above post and digging through the code and functions i was provided from the previous developer i found it to be a bit of sloppy code that runs an if statement on the value while prepping it. As such of course 0 will be identified as false, hence no value.

Thanks for the reply anyhow. I should have hit the debugging first, rather than trust supplied code!
N


Storing "0" Zero in a session as a variable - El Forum - 07-18-2011

[eluser]attos[/eluser]
This might be a little bit late, but there are differences among the following:

Code:
if($something){...}

if($something = TRUE){...}

if($something == TRUE){...}

if($something === TRUE){...}

The last one is the one preferred to avoid any ambiguity.

Cheers!


Storing "0" Zero in a session as a variable - El Forum - 07-18-2011

[eluser]nevsie[/eluser]
Hi Attos,
Yeah this basically is the method to resolve it, there are some pretty good tables and charts out there defining what passes and fails these checks - some not as obvious as others!

After all this the client has changed their mind again anyway - so another method is going to be needed, so i can happily rewrite the bits i did not like from the provided code!
Thanks, N


Storing "0" Zero in a session as a variable - El Forum - 07-18-2011

[eluser]attos[/eluser]
@nevsie,
Nothing better than your own code.
:-)