PDA

View Full Version : PHP Arrays not listing correctly



W92Baj
October 5th, 2008, 09:29 PM
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); //strip dupes
krsort($viewedlist); //reverse keys
$_SESSION['viewed'] = $viewedlist; //reset session array

My debuggery tells me that this is working as should.
Output:


array(2) { [0]=> string(1) "3" [1]=> string(1) "2" } // pre reverse
array(2) { [1]=> string(1) "2" [0]=> string(1) "3" } //post reverse

Again, so far so good.
Heres where things go a bit wonk.


$count = count($viewedlist);
if ($count >= 4)
{$count = 4;} // Count number of fields in array and crop to 4 if more

for ($i=0;$i<$count;$i++)
{
$id = $viewedlist[$i]; //Set id to array value
$query = "SELECT foo1, foo2 FROM bar WHERE id='$id' ";
$result = mysql_query($query);
$viewedrow = mysql_fetch_array($result); //query sql for data as per id
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.

nobody
October 6th, 2008, 02:04 AM
Just slap an ODER BY id DESCENDING in your SQL and you should be fine, no?

W92Baj
October 6th, 2008, 07:07 AM
No. That would list by id. I want to list by array key descending.

MikeDS
October 6th, 2008, 10:00 AM
This is a pure guess, but your for loop is starting from 0, so it could be that it's reading the item in your array with key 0 first. Maybe when you reverse them try and, er 're-key' them and see if that makes any difference...

tfg
October 6th, 2008, 11:08 AM
it's because krsort reverses the array but doesn't assign new keys. thus when you're using your for($i =0) loop, it's still accessing $array[0] as the first array element. use array_reverse instead to re-assign the keys. or use foreach($array AS $key => $value) as your loop.

W92Baj
October 6th, 2008, 01:44 PM
I dont want it to assign a new key.
The latest id should have the highest numbered key and as such I want that to be displayed first as it is the most recent.

I did have a brainwave that it might be to do with the for loop params but I am not sure what.

tfg
October 7th, 2008, 06:26 AM
I did have a brainwave that it might be to do with the for loop params but I am not sure what.you don't need a brainwave, you need to listen to what i'm saying. i gave you an explanation as well as two different solutions to try.


The question is, obviously, why?

array(2) { [1]=> string(1) "2" [0]=> string(1) "3" } //post reverse

when you then use for ($i = 0; $i < count($array); $i++) it will iterate through and pick up $array[0] first, regardless of if it's at the beginning of the array or (after krsort) at the end. then it will display $array[1] next and display that. so at the moment the way you're attempting to do things the krsort function has no impact on the array's output. if you want to use the loop you've used then you need to reassign the array keys.

why does the latest one need to have the highest array key? why can't the latest one be 0? the data's still stacked in the same order either way.

here's a third possible solution
for($i = count($array); $i = 0; $i--)