smallc28 Posted October 13, 2013 Share Posted October 13, 2013 Hello I've created an recently viewed Items list it works but I'm having two(2) problems.1. session repeats the same Id2. session goes on and on and on.....My questions are:1. how would I set it so that it doesn't repeat the same session/Id again?2. how may I set the maximum limit to 5? I've attach an Image file of what I'm trying to say anyone doesn't understand.<?php session_start();?><?php$_SESSION['recentlyViewed'] .='<table width="50" cellspacing="0" cellpadding="0" border="1"><tr><th width="50" height="50" scope="col"><a href="product.php?id=' . $id . '"><img src="inventory_images/' . $id . '.jpg" width="50" height="50" border="0" /></a></th></tr></table>';if(isset($_SESSION['recentlyVieweded']))$_SESSION['recentlyVieweded']=$_SESSION['recentlyVieweded']+1;else$_SERVER['recentlyVieweded']= 1;echo "".$_SESSION['recentlyViewed']."";?> Quote Link to comment https://forums.phpfreaks.com/topic/282940-help-setting-an-limit-to-my-recent-items/ Share on other sites More sharing options...
Ch0cu3r Posted October 13, 2013 Share Posted October 13, 2013 (edited) The problem is you're just concatenating the recently viewed item in the session variable. You don't have any logic to limit how many items get added or to filter any duplicates I'd do something like this when adding recently viewd items // Set how many recently viewed items can be added to the array $maxItems = 5; if(!isset($_SESSION['recentlyViewed'])) $_SESSION['recentlyViewed'] = array(); // if item is not already in the array, add it if(!in_array($id, $_SESSION['recentlyViewed'])) { $_SESSION['recentlyViewed'][] = $id; } // product is in array and is not the last one (most recent one) // then remove it and make it the most recent if(in_array($id, $_SESSION['recentlyViewed']) && !$_SESSION['recentlyViewed'][$maxItems - 1] == $id) { $key = array_search($id, $_SESSION['recentlyViewed']); // get the position of the old one unset($_SESSION['recentlyViewed'][$key]); // delete the item from the array $_SESSION['recentlyViewed'][] = $id; // make it the most recent } // limit the number of recently items to $maxItems $recentItems = count($_SESSION['recentlyViewed']); // how many items in the array if($recentItems > $maxItems) { $offset = $recentItems - $maxItems; array_splice($_SESSION['recentlyViewed'], $offset, $maxItems;); // make $_SESSION['recentlyViewed'] have only $maxItems in array } Then to display the recently viewed items i'd do // display recent items // most recent should be displayed first $items = array_reverse($_SESSION['recentlyViewed']); foreach($items as $id) { echo ' <table width="50" cellspacing="0" cellpadding="0" border="1"> <tr> <th width="50" height="50" scope="col"> <a href="product.php?id=' . $id . '"><img src="inventory_images/' . $id . '.jpg" width="50" height="50" border="0" /></a> </th> </tr> </table>'; } Edited October 13, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282940-help-setting-an-limit-to-my-recent-items/#findComment-1453801 Share on other sites More sharing options...
Ch0cu3r Posted October 13, 2013 Share Posted October 13, 2013 (edited) I over complicated this part, change // limit the number of recently items to $maxItems $recentItems = count($_SESSION['recentlyViewed']); // how many items in the array if($recentItems > $maxItems) { $offset = $recentItems - $maxItems; array_splice($_SESSION['recentlyViewed'], $offset, $maxItems;); // make $_SESSION['recentlyViewed'] have only $maxItems in array } to // limit number of recently viewed its ot $maxItems if(count($_SESSION['recentlyViewed']) > $maxItems) { array_shift($_SESSION['recentlyViewed']); // make $_SESSION['recentlyViewed'] have only $maxItems in array } Edited October 13, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282940-help-setting-an-limit-to-my-recent-items/#findComment-1453803 Share on other sites More sharing options...
Solution smallc28 Posted October 13, 2013 Author Solution Share Posted October 13, 2013 Thanks man you are a big help im not so good with session so thanks once again man Quote Link to comment https://forums.phpfreaks.com/topic/282940-help-setting-an-limit-to-my-recent-items/#findComment-1453804 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.