Having some issues, basically I have a random image gallery.
This is the code for the random thumbnails:
<?php
include('../../connect.php');
$result = mysql_query("SELECT *
FROM picture AS r1
JOIN
(
SELECT ROUND(RAND() * (SELECT MAX(id)
FROM picture) ) AS id
) AS r2
WHERE r1.id >= r2.id
AND public_approved=1
ORDER BY r1.id ASC
LIMIT 4;")
or die(mysql_error());
while($row = mysql_fetch_array( $result ))
{
echo "<div style='float:left; margin:2px;' >";
echo '<a href="pictures.php?id=' . $row['id'] .'"><img src="../../files/small/thumb0_'. $row['file_name'] . '.' . $row['file_extension'] . '" border="0"></br>';
echo "</div>";
}
?>
When one of the thumbnails is clicked the page reloads and uses the following command to load the main content:
<?php
include('../../connect.php');
$passed_id = $_GET['id'];
$result1 = mysql_query("SELECT * FROM picture where id='$passed_id' ")
or die(mysql_error());
while($row = mysql_fetch_array( $result1 ))
{?>
This continues on through a table displaying the larger image and title and so on.
I’m experiencing two problems:
1 – Even though I specify 4 in the limit, sometimes only 1 2 or 3 thumbs are displayed.
2 – Sometimes when I click on a thumbnail the wrong id is used, i.e. thumbnail id 2 but firing id 8.
Suppose X is the number of images you want to display, you should really take [MAX(id) – X] as the RAND() multiplier. This way the result set will have enough results.
Second problem seems simple, someone will probably post a good answer, but keep in mind it’s usually a column/parameter confusion in your php/html code.
Good luck!