sasori Posted February 23, 2012 Share Posted February 23, 2012 am currently building the cart system of a product, now there is this part wherein, the non-logged-in OR logged-in user should also be able to see the items that he/she viewed, how to do that?., am not asking for code snippets , just give me some ideas/hints/strategies/tips that may help me get the big picture on how to do this thing and proceed coding. Quote Link to comment https://forums.phpfreaks.com/topic/257605-you-viewed-items-strategy/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 23, 2012 Share Posted February 23, 2012 You would use a session variable ($_SESSION['viewed'][n] = true;, where n is a product id) to remember the product id's that the person has viewed. To display the product information, use array_keys to get the list of id's from the $_SESSION['viewed'] array. Quote Link to comment https://forums.phpfreaks.com/topic/257605-you-viewed-items-strategy/#findComment-1320334 Share on other sites More sharing options...
sasori Posted February 23, 2012 Author Share Posted February 23, 2012 You would use a session variable ($_SESSION['viewed'][n] = true;, where n is a product id) to remember the product id's that the person has viewed. To display the product information, use array_keys to get the list of id's from the $_SESSION['viewed'] array. ok, if that's the case, what if the person viewed alot of items? , i think it's uncool to paginate the data in the small box, what to do next ? or there's a better way? Quote Link to comment https://forums.phpfreaks.com/topic/257605-you-viewed-items-strategy/#findComment-1320335 Share on other sites More sharing options...
PFMaBiSmAd Posted February 23, 2012 Share Posted February 23, 2012 If you only want to keep a specific number of recent items, you would push the values onto the end of an array and only keep the last n items - <?php $n = 5; // number of recent items to remember if(isset($_GET['id'])){ $id = (int)$_GET['id']; if(!in_array($id,$_SESSION['viewed'])){ $_SESSION['viewed'][] = $id; $_SESSION['viewed'] = array_slice($_SESSION['viewed'],-$n,$n); // keep last n entries } } Quote Link to comment https://forums.phpfreaks.com/topic/257605-you-viewed-items-strategy/#findComment-1320339 Share on other sites More sharing options...
l0gic Posted February 23, 2012 Share Posted February 23, 2012 You could also prioritize items that have been viewed multiple times so like above with the session variable, but instead of true/false have it count the number of times it's been viewed. I know personally I usually look at something and come back to it again several times before I buy online. You could order from most viewed to least viewed and call it something like, "Other items you've recently been interested in." Quote Link to comment https://forums.phpfreaks.com/topic/257605-you-viewed-items-strategy/#findComment-1320340 Share on other sites More sharing options...
sasori Posted February 23, 2012 Author Share Posted February 23, 2012 ok guys, I ended up with my own function //from the class file public function displayItemViewed(){ $image = ''; $catid = ''; foreach(array_keys($_SESSION['viewed']) as $item){ $image = $this->getProductImage($item); $catid = $this->getCatByProductID($item); if(count($_SESSION['viewed']) <= 5){ echo '<li> <a alt="'.$item.'" title ="'.$catid["ProductName"].'"href="catalog-details.php?prod_id='.$item.'&cat_id='.$catid["ProductCatID"].'"><img src="module/newmodule/products/'.$image['ImageLocation'].'"></a> </li>'; } else { array_shift($_SESSION['viewed']); reset($_SESSION['viewed']); } } } //inside the you also viewed items box <ul> <?=$myCart->displayItemViewed();?> </ul> //from the product details page from where I set the session $productid = $_GET['prod_id']; if(isset($productid)){ $_SESSION["viewed"][$productid] = true; } now my problem is, whenever i run everything...my function seem to display a 0 key?, when it shouldn't that's why there are some instances where my "you also viewed items" has 1 item missing in the set of five , how to fix this ? Quote Link to comment https://forums.phpfreaks.com/topic/257605-you-viewed-items-strategy/#findComment-1320404 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.