I want to list recently viewed items that the user has been to.
My diseased brain has decided to store the items id as an array in the session.
So far so good.
After checking if an array exists in the session or not I pump the current item id into the array.
The plan is to strip out any duplicates (in case the user visits the same page a couple of times), reverse the array keys so most recent is first then print the values.
$viewedlist = array_unique($viewed); [COLOR=darkorchid]//strip dupes[/COLOR]
krsort($viewedlist); [COLOR=darkorchid]//reverse keys[/COLOR]
$_SESSION['viewed'] = $viewedlist; [COLOR=darkorchid]//reset session array[/COLOR]
My debuggery tells me that this is working as should.
Output:
array(2) { [0]=> string(1) "3" [1]=> string(1) "2" } [COLOR=darkorchid]// pre reverse[/COLOR]
array(2) { [1]=> string(1) "2" [0]=> string(1) "3" } [COLOR=darkorchid]//post reverse[/COLOR]
Again, so far so good.
Heres where things go a bit wonk.
$count = count($viewedlist);
if ($count >= 4)
{$count = 4;} [COLOR=darkorchid]// Count number of fields in array and crop to 4 if more[/COLOR]
for ($i=0;$i<$count;$i++)
{
$id = $viewedlist[$i]; [COLOR=darkorchid]//Set id to array value[/COLOR]
$query = "SELECT foo1, foo2 FROM bar WHERE id='$id' ";
$result = mysql_query($query);
$viewedrow = mysql_fetch_array($result); [COLOR=darkorchid]//query sql for data as per id[/COLOR]
echo "foobar=".$id."<br />";
}
Here is the odd bit the output is
foobar=3
foobar=2
when it should be
foobar=2
foobar=3
The question is, obviously, why?
Removing the krsort makes little difference to the output.