Possible Duplicate:
Doing a while / loop to get 10 random results
I'm making a website which people can upload funny pictures they take or make. Each time the home page is loaded, a random picture/record is displayed from the MySQL database onto the page. This is done with...
$filename = mysql_fetch_row(mysql_query("SELECT filename FROM pictures ORDER BY RAND() LIMIT 1"));
echo $filename[0];
This would echo a random pictures file name onto the page. Something like funny_cat
. But then I need the file type so that I can echo out the image onto the page. Like this...
while ($record = mysql_fetch_array(mysql_query("SELECT * FROM pictures WHERE p='$p'"))) {
$filetype = $record["filetype"];
}
echo "<img src=\"picture/".$filename[0].".".$filetype."\" />";
All is good.
Now here's the bit I'm struggling on. I'm wanting to display 6 other random smaller pictures onto the page. So I need to get 6 random records and echo the filename
and filetype
for each record. The thing that needs considering is I can only get random records with the technique I used above due to the way the database is set up. How could I do this?
If there is anything you're unsure about please say and I'll try explain. Also, please explain your solutions. Thanks!! :-)