Please help!! how to compare an array with another
| From: | Jimmy Bäckström | Date: | Sun, 04 Mar 2001 23:32:52 +0000 |
| Subject: | Please help!! how to compare an array with another | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-42536@lists.php.net to get a copy of this message | ||
Shu!
I am currently working on a site where I want to randomly show pictures. I am using sessions to save
the id's of the pictures that a user has seen during his/hers visit. My first question is: Can
I use an array as a session variable? like this:
$arr = array(1, 2, 3, 4, 5);
session_start();
session_register("arr");
This array is to be passed on as an argument to a function used to find a random id, that has not
yet been seen. This array should be compared to another array with the picture id's. There is
two functions to accomplish the following: Create a randomized array consisting of all the pictures
id. Find one id in that randomized array that does not exist in the array of allready seen pictures.
These are the functions:
function show_pic($pics_voted, $cat, $sex) {
srand ((double) microtime() * 1000000);
$result = query("SELECT id FROM $cat WHERE sex='$sex'");
while ($row = mysql_fetch_row($result)) {
$arr[] = $row[0];
}
shuffle($arr);
$picture_id = find_id($pics_voted, $arr);
echo "picture_id => $picture_id";
return $picture_id;
}//show_pic
//Hitta id som inte finns bland $pics_voted[]
function find_id($pics_voted, $random, $x = 0) {
for ($i = 0; $i < count($pics_voted); $i++) {
if ($random[$x] == $pics_voted[$i] AND $x >= count($random)) return "no_pics"; //If
current key in $random[] is equal to current key in $pics_voted[] AND $x is greater than, or equal
to the number of keys in $random[], that is if the last key in $random[] has been reached, return
"no_pics"
elseif ($random[$x] == $pics_voted[$i]) {
find_id($pics_voted, $random, $x+1); //If current key in $random[] is equal to current key in
$pics_voted[] run the function again, with $x+1 as an argument to be used in the $random[] array,
that is: do the funtion again with the next key in $random[]
}//elseif
}//for
return $random[$x];
}//find_id
Does anyone know why this ain't working? I get the same id over and over again. Maybe there is
an easuer way to achive what I want?
Please help!!