Hi,
you need a little bit more:
The code in your last post just maintains the array containing the last 4 items. Change it a little bit:
$code should be a variable that checks if the customer is viewing an item.
if($viewing_an_item)
{
// new item selected
// if not set create empty array
if(!isset($_SESSION['recent_list']))
$_SESSION['recent_list'] = array();
// if set add code to beginning of array
// if code exists ignore
if(isset($_SESSION['recent_list']))
{
if (!in_array($itemid, $_SESSION['recent_list']))
array_unshift($_SESSION['recent_list'], $itemid);
}
// if total list is greater than or equal to 4
// pop one off the end
$count = (count($_SESSION['recent_list']));
if($count >= 4)
array_pop($_SESSION['recent_list']);
}
This will store an array containing the last 4 viewed items in a session.
Additionally, you need code that e.g. creates a table that displays the item info. If you just want to display a table containing a list of item names you can either create a function that fetches the needed item data from the database or from the session.
Example:
// view list
if (isset($_SESSION['recent_list']) && is_array($_SESSION['recent_list'])) {
$ids = implode(",",$_SESSION['recent_list']);
$sql = "SELECT id,name FROM table WHERE id IN ($ids)";
$res = mysql_query($sql);
while ($row = mysql_fetch_array($res)) {
// echo table containing rows with the names and links to the view
// item page using $row['id'] and $row['name']
}
}
You can do that without the need to query the database if you change the first code example above to store both the id and the item name for each item in the session array.
Thomas