denhamd2 Posted April 4, 2007 Share Posted April 4, 2007 Hi, I'm wondering could you help me... I'm sending a number of variables between 2 pages using the form GET method, and I want to store them in a session. The variables are basically called after dates and they will equal "yes" if the user has selected them. Eg. they might have selected 3 tickboxes in page 1: $07042007 = yes $18042007 = yes $22042007 = yes which get sent to page 2 via the URL is there any way of storing these in session variables of the same names? Link to comment https://forums.phpfreaks.com/topic/45580-storing-variables-in-a-session/ Share on other sites More sharing options...
kenrbnsn Posted April 4, 2007 Share Posted April 4, 2007 First, you can't have variable names that start with a digit in PHP, so those are illegal. To store a value in a $_SESSION, do <?php $var = 'yes'; $_SESSION['var'] = $var; ?> Ken Link to comment https://forums.phpfreaks.com/topic/45580-storing-variables-in-a-session/#findComment-221326 Share on other sites More sharing options...
denhamd2 Posted April 5, 2007 Author Share Posted April 5, 2007 I'm going to have a number of variables coming into the 2nd page, sometimes it might be 2 variables, other times it might be 17 variables, so is there any way I can store all the variables passed as session variables dynamically? Link to comment https://forums.phpfreaks.com/topic/45580-storing-variables-in-a-session/#findComment-221990 Share on other sites More sharing options...
jitesh Posted April 5, 2007 Share Posted April 5, 2007 This will set all your get varibles to session with same name in get if(isset($_GET)){ foreach($_GET as $key => $value){ $_SESSION[$key] = $value; } } Link to comment https://forums.phpfreaks.com/topic/45580-storing-variables-in-a-session/#findComment-221993 Share on other sites More sharing options...
denhamd2 Posted April 5, 2007 Author Share Posted April 5, 2007 great thanks is there any way I can echo all my session variable $value's then on the 2nd page (separated by <br> tags)? Link to comment https://forums.phpfreaks.com/topic/45580-storing-variables-in-a-session/#findComment-222025 Share on other sites More sharing options...
jitesh Posted April 5, 2007 Share Posted April 5, 2007 if(isset($_GET)){ foreach($_GET as $key => $value){ $_SESSION[$key] = $value; } } echo "<pre>"; print_r($_SESSION); foreach($_SESSION as $key => $value){ ......................... .......................... I think you will get this } Link to comment https://forums.phpfreaks.com/topic/45580-storing-variables-in-a-session/#findComment-222030 Share on other sites More sharing options...
denhamd2 Posted April 5, 2007 Author Share Posted April 5, 2007 Edit: figured it out just echoed the $value, easy! Only thing is it echos the value of the submit button from the first page, the name of this is monthsubmit, and the value is bookdays... any ideas on not having this stored as a session variable? Link to comment https://forums.phpfreaks.com/topic/45580-storing-variables-in-a-session/#findComment-222035 Share on other sites More sharing options...
jitesh Posted April 5, 2007 Share Posted April 5, 2007 Explain what you need to do after storing get data into session ? Link to comment https://forums.phpfreaks.com/topic/45580-storing-variables-in-a-session/#findComment-222038 Share on other sites More sharing options...
denhamd2 Posted April 5, 2007 Author Share Posted April 5, 2007 sorry mate just edited my above message, any ideas on that? Link to comment https://forums.phpfreaks.com/topic/45580-storing-variables-in-a-session/#findComment-222039 Share on other sites More sharing options...
jitesh Posted April 5, 2007 Share Posted April 5, 2007 list out the varibles you do not want to set in session of previous page which are fixed and few. like this $un_nec_varible = array('monthsubmit','anyother'); if(isset($_GET)){ foreach($_GET as $key => $value){ if(!in_array($un_nec_varible,$key)){ $_SESSION["DATA"][$key] = $value; } } } echo "<pre>"; print_r($_SESSION["DATA"]); Link to comment https://forums.phpfreaks.com/topic/45580-storing-variables-in-a-session/#findComment-222041 Share on other sites More sharing options...
denhamd2 Posted April 5, 2007 Author Share Posted April 5, 2007 getting the error msg for each of the variables: Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/aholiday/public_html/calendar/index2.php on line 9 Link to comment https://forums.phpfreaks.com/topic/45580-storing-variables-in-a-session/#findComment-222043 Share on other sites More sharing options...
jitesh Posted April 5, 2007 Share Posted April 5, 2007 if(!in_array($key,$un_nec_varible)){ Link to comment https://forums.phpfreaks.com/topic/45580-storing-variables-in-a-session/#findComment-222045 Share on other sites More sharing options...
denhamd2 Posted April 5, 2007 Author Share Posted April 5, 2007 something strange happens, it just echos the word Array any ideas? Link to comment https://forums.phpfreaks.com/topic/45580-storing-variables-in-a-session/#findComment-222052 Share on other sites More sharing options...
jitesh Posted April 5, 2007 Share Posted April 5, 2007 print_r is used to print array and object. echo "<pre>"; print_r($_SESSION["DATA"]); Link to comment https://forums.phpfreaks.com/topic/45580-storing-variables-in-a-session/#findComment-222055 Share on other sites More sharing options...
denhamd2 Posted April 5, 2007 Author Share Posted April 5, 2007 sorted mate thanks Link to comment https://forums.phpfreaks.com/topic/45580-storing-variables-in-a-session/#findComment-222063 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.