TheJoey Posted October 10, 2009 Share Posted October 10, 2009 Just wondering if there is a way to parse information stored in session and put it into a .txt file. echo '<pre>'; print_r($_SESSION); echo '</pre>'; i get this information about my session Array ( [logins] => 1 [item] => item1 [price] => 70 [itemname] => Array ( [item1] => item1 ) [itemqty] => Array ( [item2] => 3 ) [itemprice] => Array ( [item1] => 70 ) [qty] => 3 ) just wondering if this can be done and how i would go about doing so. Quote Link to comment https://forums.phpfreaks.com/topic/177211-solved-saving-session-info/ Share on other sites More sharing options...
nuttycoder Posted October 10, 2009 Share Posted October 10, 2009 you can get the data from a session by using the session name and adding it to a variable like this: $data = $_SESSION['item']; Then you can save that information into a text file $file = "session.txt";//set a name for a text file will be created if it does not exist $fo = fopen($file, 'w');//open the file fwrite($fo, $data);//write data there fclose($fo);//close file Quote Link to comment https://forums.phpfreaks.com/topic/177211-solved-saving-session-info/#findComment-934413 Share on other sites More sharing options...
cags Posted October 10, 2009 Share Posted October 10, 2009 Just wondering if there is a way to parse information stored in session and put it into a .txt file. Yes. How you would do it would be entirely up to you and what order/structure you wished the output to have. Edit: nuttycoder just showed the technique for doing so. One thing I can say looking at what you've got, that doesn't appear to me to be the most sensible way of keeping the information in your $_SESSION array. I'd have thought the items would make more sensed stored like so... <?php $items = array( array('name'=>"Name of item 1", 'price'=>"Price of item 1", 'quantity'=>"Quantity of item 1"), array('name'=>"Name of item 2", 'price'=>"Price of item 2", 'quantity'=>"Quantity of item 2"), array('name'=>"Name of item 3", 'price'=>"Price of item 3", 'quantity'=>"Quantity of item 3"), ); ?> I guess thats by-the-by though. Quote Link to comment https://forums.phpfreaks.com/topic/177211-solved-saving-session-info/#findComment-934417 Share on other sites More sharing options...
TheJoey Posted October 11, 2009 Author Share Posted October 11, 2009 The saving of the session works fine but it doesnt seem to get the information i want Array ( [login] => 1 ( [item] => item [item1] => item1 [item2] => item2 ) [itemqty] => Array ( [item] => 16 [item1] => 22 [item2] => 23 ) [itemprice] => Array ( [item] => 150 [item1] => 60 [item2] => 40 ) ) thats how the array looks. how do i extract all of the itemprice array because when i do it just echos 'array' Quote Link to comment https://forums.phpfreaks.com/topic/177211-solved-saving-session-info/#findComment-934668 Share on other sites More sharing options...
cags Posted October 11, 2009 Share Posted October 11, 2009 foreach($_SESSION['itemprice'] as $item_price) { echo $item_price; } Quote Link to comment https://forums.phpfreaks.com/topic/177211-solved-saving-session-info/#findComment-934806 Share on other sites More sharing options...
TheJoey Posted October 11, 2009 Author Share Posted October 11, 2009 foreach($_SESSION['itemprice'] as $item_price) { echo '$'.$item_price."\r\n"; } echo "\r\n"; foreach($_SESSION['itemqty'] as $itemqty) { echo '#'.$itemqty."\r\n"; } echo "\r\n"; foreach($_SESSION['itemname'] as $itemname) { echo 'Name:'.$itemname."\r\n"; } how would i make it display itemname : item_price : itemqty when i tried i just got a bunch of errors, i even tried joining the foreach's Quote Link to comment https://forums.phpfreaks.com/topic/177211-solved-saving-session-info/#findComment-934857 Share on other sites More sharing options...
cags Posted October 11, 2009 Share Posted October 11, 2009 IF you'd have changed the array to the format I suggested earlier you could have done... foreach($_SESSION['items'] as $item){ echo $item['name'] . ', ' . $item['price'] . ', ' . $item['quantity']; } Using the more confusing pattern you have, you would have to do something like... foreach($_SESSION['itemname'] as $k=>$v) { echo $v . ', ' . $_SESSION['itemprice'][$k] . ', ' . $_SESSION['itemqty'][$k]; } Quote Link to comment https://forums.phpfreaks.com/topic/177211-solved-saving-session-info/#findComment-934863 Share on other sites More sharing options...
TheJoey Posted October 11, 2009 Author Share Posted October 11, 2009 foreach($_SESSION['items'] as $item){ echo $item['name'] . ', ' . $item['price'] . ', ' . $item['quantity']; } that gives me this Warning: Invalid argument supplied for foreach() Quote Link to comment https://forums.phpfreaks.com/topic/177211-solved-saving-session-info/#findComment-934871 Share on other sites More sharing options...
TheJoey Posted October 11, 2009 Author Share Posted October 11, 2009 second foreach works fine thanks. Quote Link to comment https://forums.phpfreaks.com/topic/177211-solved-saving-session-info/#findComment-934873 Share on other sites More sharing options...
cags Posted October 11, 2009 Share Posted October 11, 2009 Of course it does, it doesn't apply to your code. IF you'd have changed the array to the format I suggested earlier you could have done... foreach($_SESSION['items'] as $item){ echo $item['name'] . ', ' . $item['price'] . ', ' . $item['quantity']; } Quote Link to comment https://forums.phpfreaks.com/topic/177211-solved-saving-session-info/#findComment-934875 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.