In order to prove (to myself), I could do this, I produced the following on the product detail page, (which takes the url parameter ‘Item’).
It may need some tidying up, but it seems to work ok
This code creates an array stored as session variable to hold the last 6 itemid’s
The array is then sliced to remove the current itemid, so we end up with 5 elements for setting the criteria.
A criteria string is then built to append to the select statement, which returns 5 rows, for the last 5 items viewed.
(you could also do some further inspection of the array, to eliminate any duplicate key values, but i have't yet done this)
The only problem is in the order of the resulting rows.
I can’t think of a way to sort the rows in the order in which they were stored in the array.
Can anyone help with this, or come up with an alternative solution ?
<?php
if (!isset ($_SESSION["lastviewed"])) {//set up array to hold item id of selected products
$_SESSION["lastviewed"] = array();
}
if (isset($_GET['Item'])) {//adds a itemid to the array
if (count($_SESSION["lastviewed"]) <= 5) {//we only want 6 elements to the array, so add one if we currently have 5 or fewer
array_push($_SESSION["lastviewed"],(get_magic_quotes_gpc()) ? $_GET['Item'] : addslashes($_GET['Item']));//add the current itemid to the array
} else {
$_SESSION["lastviewed"] = array_slice($_SESSION["lastviewed"],1) ;// we have 6 elements already, so remove the first element of the array
array_push($_SESSION["lastviewed"],(get_magic_quotes_gpc()) ? $_GET['Item'] : addslashes($_GET['Item']));//add the current itemid to the array
}
}
//create the criteria string
$_SESSION["criteria"] = array_slice($_SESSION["lastviewed"],0,-1) ;// we don't want the current itemid, so remove the last element of the 'lastviewed' array
$stringcriteria = '';
foreach ($_SESSION["criteria"] as $key => $value) {//loop through the array to build the string to set the criteria
$stringcriteria = $stringcriteria.' OR ItemID = '.$value;
}
$stringcriteria = substr($stringcriteria,4) ;//the string needs to be trimmed, to remove ' OR ' from the beginning
mysql_select_db($databaseConnection, $Connection);
$query_rslastfiveviewed = "SELECT ItemID, ItemName, ItemImageURL, ItemThumbnailURL FROM tblitem WHERE ".$stringcriteria."";
$rslastfiveviewed = mysql_query($query_rslastfiveviewed, $Connection) or die(mysql_error());
$row_rslastfiveviewed = mysql_fetch_assoc($rslastfiveviewed);
$totalRows_rslastfiveviewed = mysql_num_rows($rslastfiveviewed);
?>
...add the appropriate code to the page to view the results