Hello, I am working on a script where random images show up and people rate them. I am having one problem though. That is that the same images keep showing up! Does anyone know how to go about this? I was thinking about using cookies but I am totally lost! Any ideas? Thank You!
Where are you getting the images from? Could you provide the code you are currently using? Would be helpful.
the images are just in a folder called images. The only code i have is to select a random picture from my database, thats all I know how to do so far. $query = "SELECT * FROM pictures"; $result = MYSQL_QUERY($query); $randval = rand(0, mysql_num_rows($result) - 1); $Rand_PID = mysql_result($result, $randval, 0); // Gets the Picture ID number PHP:
Ah, ok. Loop through the dir with opendir and readdir and add the files to a array. When you did that, use rand.
Try something like this: <?php $directory = '/images/'; if ($handle = opendir($directory)){ $files = $array(); while (false !== ($file = readdir($handle))){ $files[] = $file; } } $random_image = rand(0, (count($files)-1)); ?> PHP:
If you really need to fetch image as an SQL resut you could try something like this: // Count total number of rows and place it in $records $query = "SELECT COUNT(*) as count FROM pictures"; list ($records) = mysql_fetch_assoc(mysql_query($query)); // Generate random offset (0<$offset<$records) for query $offset = rand(0, $records - 1); // Finally, fetch random row result $query = "SELECT * FROM pictures LIMIT {$offset},1"; $Rand_PID = mysql_fetch_assoc(mysql_query($query)); PHP:
What about having a query like this, and use the RAND() function in MySQL. $query = mysql_query("SELECT * FROM `pictures` ORDER BY RAND() LIMIT 1;") or die(mysql_error()); PHP: then basically, fetch the information of that image.
That solution seems definitely better than mine, I actually didn't know that the RAND() function can be used in that way.
The more rows there are in your database table, the worse ORDER BY RAND will perform. Selecting a row based on the count of rows in a table times a random float works great. this seems to work a little faster though because it's using one query instead of two and the second query takes the same amount of time as a normal WHERE id=N query.
I will say this will be better: 1) get picture count 2) do random from 1 to picture count 3) get picture by random selected id maybe it will lower server load!
Until you start noticing that your site is slow, it's best to just use the standard "ORDER BY RAND()" solution. Many people claim this is slow, and it was, but I don't know if it still is. Has anyone actually bothered to test this again in newer versions of mysql? It would seem to me that this would be something the mysql guys would bother optimizing (if it is at all possible)
It's not the same answer, it's how to do it if you have to use MySQL resource. This is not a competition, Sky AK47, everyone is trying their best.
And if you could read, you may be able to read the code he posted here. He does use MySQL from the snippet of code he posted.
But he didn't write the preferred solution, MySQL or directory reading, in which case the globbing could also do the trick $rndImg = $rndImg[array_rand($rndImg = glob("images/*"))]; PHP:
I can read, too bad you can't Take a look at what premiumscripts posted. @Gray Fox: there are various way to do this, but at least your back on topic!