//get highest value in list
$maxnum="SELECT * FROM bl_photos ORDER BY photoid DESC LIMIT 1";
$maxnum2=mysql_query($maxnum) or die("Could not get num<br>".mysql_error());
$maxnum3=mysql_fetch_array($maxnum2);
//get 2 random values from list
$randimg=mt_rand(1, $maxnum3[photoid]);
$randimg2=mt_rand(1, $maxnum3[photoid]);
//check if $randimg exists
$checkrand="SELECT * FROM bl_photos WHERE photoid='$randimg' LIMIT 1";
$checkrand2=mysql_query($checkrand) or die("Could not get num 1<br>".mysql_error());
$checkrand3=mysql_num_rows($checkrand2);
//if not, get a new value (this is where the code fucks up)
while($checkrand3==0) {
$randimg=mt_rand(1, $maxnum3[photoid]);
}
//check for $randimg2
$checkrand4="SELECT * FROM bl_photos WHERE photoid='$randimg2' LIMIT 1";
$checkrand5=mysql_query($checkrand4) or die("Could not get num 2<br>".mysql_error());
$checkrand6=mysql_num_rows($checkrand5);
//if not, get new value (also this one stinks)
while($checkrand6==0) {
$randimg2=mt_rand(1, $maxnum3[photoid]);
}
//check if their not the same, if so loop till their not (works)
while($randimg==$randimg2) {
$randimg2=mt_rand(1, $maxnum3[photoid]);
}
//if all is right get the 2 images from db (images are NOT stored in the db, just a id, name, path and description)
$img="SELECT * FROM bl_photos WHERE photoid='$randimg' LIMIT 1";
$img2=mysql_query($img) or die("Could not get image 1<br>".mysql_error());
$img3=mysql_fetch_array($img2);
$img4="SELECT * FROM bl_photos WHERE photoid='$randimg2' LIMIT 1";
$img5=mysql_query($img4) or die("Could not get image 2<br>".mysql_error());
$img6=mysql_fetch_array($img5);
//create a full path
$source1="../images/gallery/$img3[picture]";
$source2="../images/gallery/$img6[picture]";
//show images
print "<br><tr class='headline'><td>Random photo</td></tr>";
print "<tr><td class='rowleft' align='center'><br>";
print "<a href='showpic.php?photoID=$img3[photoid]&albumID=$img3[album]'><img src='mods/thumbphoto.php?s=$source1&w=150&h=150' alt='' border='0'></a>";
print "</td><td class='rowright' align='center'><br>";
print "<a href='showpic.php?photoID=$img6[photoid]&albumID=$img6[album]'><img src='mods/thumbphoto.php?s=$source2&w=150&h=150' alt='' border='0'></a>";
print "</td></tr></table>";
the goal is to get 2 random images from the list (which is stored in mysql) and show them on a page. though the 2 images can never be the same image at the same time.
therefor i inserted these three while statements
but most of the time the script timeouts after 30 seconds
$checkrand="SELECT * FROM bl_photos WHERE photoid='$randimg' LIMIT 1";
$checkrand2=mysql_query($checkrand) or die("Could not get num 1<br>".mysql_error());
$checkrand3=mysql_num_rows($checkrand2);
while($checkrand3==0) {
$randimg=mt_rand(1, $maxnum3[photoid]);
}
$checkrand4="SELECT * FROM bl_photos WHERE photoid='$randimg2' LIMIT 1";
$checkrand5=mysql_query($checkrand4) or die("Could not get num 2<br>".mysql_error());
$checkrand6=mysql_num_rows($checkrand5);
while($checkrand6==0) {
$randimg2=mt_rand(1, $maxnum3[photoid]);
}
while($randimg==$randimg2) {
$randimg2=mt_rand(1, $maxnum3[photoid]);
}
I am aware that this way of doing it, performance can be slow, if anyone knows a way to make it faster.... well, all ideas are welcome 🙂
can anyone review the code and tell me what should be changed? Thanks!