dfowler Posted January 15, 2008 Share Posted January 15, 2008 Hey guys, I'm pulling a bunch of information from a database in a loop. As I pull the information I want to store them in a $_SESSION variable. Here is my code so far: $query = "SELECT * FROM table where id='$k'"; $result = mysql_query($query); while($row = mysql_fetch_array($result)) { $_SESSION['item_name'] = $row['name']; } The problem is that it is only saving the last item. I'm not sure how to do this correctly as using session variables are rather new to me. Quote Link to comment https://forums.phpfreaks.com/topic/86206-solved-session-variables-in-loop/ Share on other sites More sharing options...
papaface Posted January 15, 2008 Share Posted January 15, 2008 $query = "SELECT * FROM table where id='$k'"; $result = mysql_query($query); while($row = mysql_fetch_array($result)) { foreach ($row as $key => $value) { $_SESSION[$key] = $value; } } Quote Link to comment https://forums.phpfreaks.com/topic/86206-solved-session-variables-in-loop/#findComment-440270 Share on other sites More sharing options...
trq Posted January 15, 2008 Share Posted January 15, 2008 Assuming your only looking for the data related to one member, there may only be one record. No need for a while loop. <?php session_start(); $query = "SELECT * FROM table where id='$k' LIMIT 1"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { $row = mysql_fetch_array($result); foreach ($row as $k => $v) { $_SESSION[$k] = $v; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/86206-solved-session-variables-in-loop/#findComment-440272 Share on other sites More sharing options...
dfowler Posted January 15, 2008 Author Share Posted January 15, 2008 No, there will be multiple data. The user will can pick either one or multiple items that they want to look at. It's going to be a shopping cart system. Screen 1 The user picks what they want to look at Screen 2 shows what they picked on screen #1 Screen 3 processes checkout Quote Link to comment https://forums.phpfreaks.com/topic/86206-solved-session-variables-in-loop/#findComment-440282 Share on other sites More sharing options...
trq Posted January 15, 2008 Share Posted January 15, 2008 Then your going to need to append something to the $key on each iteration of your while loop. eg; <?php session_start(); $index = 0; $query = "SELECT * FROM table where id='$k'"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_array($result)) { foreach ($row as $k => $v) { $_SESSION[$k . '_' . $index] = $v; $index++; } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/86206-solved-session-variables-in-loop/#findComment-440286 Share on other sites More sharing options...
dfowler Posted January 16, 2008 Author Share Posted January 16, 2008 Thanks for your help so far! I assume this worked as I didn't get any error on the page and it loaded correctly. As I stated before, I have never used session variables before. Now that the information is in there, how do I retrieve it? Quote Link to comment https://forums.phpfreaks.com/topic/86206-solved-session-variables-in-loop/#findComment-440898 Share on other sites More sharing options...
adam291086 Posted January 16, 2008 Share Posted January 16, 2008 just do echo $SESSION['your session name here'] Quote Link to comment https://forums.phpfreaks.com/topic/86206-solved-session-variables-in-loop/#findComment-440905 Share on other sites More sharing options...
dfowler Posted January 16, 2008 Author Share Posted January 16, 2008 I'm just really messing this stuff up I think, because I have no idea what the name would be. Quote Link to comment https://forums.phpfreaks.com/topic/86206-solved-session-variables-in-loop/#findComment-440920 Share on other sites More sharing options...
adam291086 Posted January 16, 2008 Share Posted January 16, 2008 i think it will be something like $SESSION[iD number_1'] The one will change everythime the loop runs thorugh, it will stop when theres no more data from the database query result Quote Link to comment https://forums.phpfreaks.com/topic/86206-solved-session-variables-in-loop/#findComment-440922 Share on other sites More sharing options...
dfowler Posted January 16, 2008 Author Share Posted January 16, 2008 So I should be able to come up with another loop to process the information into the database? I'm sorry to be so dumb about this. When I started this process I thought it would be a lot easier than it has become. Quote Link to comment https://forums.phpfreaks.com/topic/86206-solved-session-variables-in-loop/#findComment-440965 Share on other sites More sharing options...
dfowler Posted January 16, 2008 Author Share Posted January 16, 2008 I couldn't get it to work right, so I ended up as the objects were pulled in the initial loop putting them in a hidden input field. Then I just posted them to the next screen. Quote Link to comment https://forums.phpfreaks.com/topic/86206-solved-session-variables-in-loop/#findComment-441089 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.