johnrb87 Posted August 25, 2010 Share Posted August 25, 2010 Hello everyone I wonder if you could help me. I want to create a function called <?php function mythings($what) Which would allow me to pass an ID number to, such as <?php mythings(5419); What I want to do with that function is store the ID numbers of the past 6 items I have viewed, but I want to store each item as a SESSION once it has been passed. So essentially I would end up with the following 6 SESSIONS <?php $_SESSION['item1']; $_SESSION['item2']; $_SESSION['item3']; $_SESSION['item4']; $_SESSION['item5']; $_SESSION['item6']; So the "item1" SESSION would be the latest item ID passed through the function But then if I then passed a new ID through the function, that new ID passed would then become $_SESSION['item1']; and the current SESSION $_SESSION['item1']; would become $_SESSION['item2']; and $_SESSION['item2']; would become $_SESSION['item3']; and so on. This means I can do a list such as <?php print "Item 1".$_SESSION['item1']."<br>"; print "Item 2".$_SESSION['item2']."<br>"; print "Item 3".$_SESSION['item3']."<br>"; print "Item 4".$_SESSION['item4']."<br>"; print "Item 5".$_SESSION['item5']."<br>"; print "Item 6".$_SESSION['item6']."<br>"; ?> Does that make much sense? Can anyone help. Thanks very much all John Quote Link to comment https://forums.phpfreaks.com/topic/211672-storing-last-6-ids-viewed-as-sessions/ Share on other sites More sharing options...
Alex Posted August 25, 2010 Share Posted August 25, 2010 It would be much easier to use an array and do it like this: function mythings($id) { if(!isset($_SESSION['items']) { $_SESSION['items'] = array($id); } else { array_unshift($_SESSION['items'], $id); if(sizeof($_SESSION['items'] > 6)) { $_SESSION['items'] = array_slice($_SESSION['items'], 0, 6); } } } And then to print it out: foreach($_SESSION['items'] as $key => $item) { echo "Item " . ($key + 1) . ": " . $item . "<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/211672-storing-last-6-ids-viewed-as-sessions/#findComment-1103457 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.