Jump to content

Help setting an limit to my recent items


smallc28

Recommended Posts

Hello 
I've created an recently viewed Items list it works but I'm having two(2) problems.
1. session repeats the same Id
2. 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']."";
?> 

post-133640-0-94421000-1381689604_thumb.jpg

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>';
}

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
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.