Hi,

I am a relative newbie to PHP. I am currently building a photo gallery for an events website and I have most of the backend finished.

I am having a very basic problem with the homepage of the gallery.

Basically, the way I am designing is in a table with 2 columns;
- a column with a cover image for each album
- a column with thumbnails from that album

I want to display on the albums/image like this in rows. This is where I am having the problem...
I have the album cover image working fine but for some reason the same images are showing up for each album. I'm pretty sure it has something to do with a 'while' loop and the album id.

Below is the PHP code

I know this a basic problem, but help would be MUCH appreciated!!

Thanks,

Paul

PHP Code:

<table border="0" cellpadding="5" cellspacing="0">

<?

$db_cnx = mysql_connect ('localhost', 'user', 'password');
$db_select = mysql_query ('USE gallery');

$sql = "SELECT al_id, al_name, al_image, COUNT(im_album_id) AS al_numimage
FROM tbl_album al LEFT JOIN tbl_image im ON al.al_id = im.im_album_id
GROUP by al_id
ORDER BY al_name";

$result = mysql_query($sql) or die('Error, list album failed. ' . mysql_error());

/while ($get_id = mysql_fetch_assoc($result)) { echo $get_id['al_id'];}/
while ($get_id = mysql_fetch_assoc($result)) { $albumid = $get_id['al_id'];}

$result = mysql_query($sql) or die('Error, list album failed. ' . mysql_error());

while ($row = mysql_fetch_assoc($result)) {

/$numImages = $row['al_numimage'] > 1 ? $row['al_numimage'] . ' images' : $row['al_numimage'] . ' image';/

echo /ECHO THE ALBUMS/
'<tr>'.
'<td>'.
'<a href="index.php?page=list-image&album=' . $row['al_id'] . '">' .
'<img src="viewImage.php?type=album&name=' . $row['al_image'] . '" border="0">' .
'<br>' . $row['al_name'] . '</a><br />' . $numImages .
'</td>';

/START GETTING THE IMAGES/

$query2 = "SELECT im_id, im_title, im_thumbnail
FROM tbl_image
WHERE im_album_id = '$albumid'
ORDER BY im_title";

$result2 = mysql_query($query2) or die('Error, list image failed. ' . mysql_error());

while ($row2 = mysql_fetch_assoc($result2)) {
echo
/ECHO THE IMAGES/
'<td>'.
'<a href="?page=image-detail&album=' . $albumId . '&image=' . $row2['im_id'] . '">' .
'<img src="viewImage.php?type=glthumbnail&name=' . $row2['im_thumbnail'] . '" border="0">' .
'</td>';

}
echo '</tr>';

}

?>

</table>

    while ($get_id = mysql_fetch_assoc($result)) { 
    	$albumid = $get_id['al_id'];
    }
    
    $result = mysql_query($sql) or die('Error, list album failed. ' . mysql_error());
    
    while ($row = mysql_fetch_assoc($result)) {
    

    Both while loops use a result from the same query. Why are they in different while loops?

    In your code $albumid will always be set to the value of 'al_id' in the last record. Thus, you get the same thumbnail image each time you loop through the next while loop. You can consolidate them and get the current value of 'al_id' for the current record...

    <table border="0" cellpadding="5" cellspacing="0">
    <?
    $db_cnx = mysql_connect ('localhost', 'user', 'password');
    $db_select = mysql_query ('USE gallery');
    
    $sql = "SELECT al_id, al_name, al_image, COUNT(im_album_id) AS al_numimage
    FROM tbl_album al LEFT JOIN tbl_image im ON al.al_id = im.im_album_id
    GROUP by al_id
    ORDER BY al_name";
    
    /*$result = mysql_query($sql) or die('Error, list album failed. ' . mysql_error());
    
    while ($get_id = mysql_fetch_assoc($result)) { 
    	$albumid = $get_id['al_id'];
    }*/
    
    $result = mysql_query($sql) or die('Error, list album failed. ' . mysql_error());
    
    while ($row = mysql_fetch_assoc($result)) {
    
    $albumid = $row['al_id'];
    
    /*$numImages = $row['al_numimage'] > 1 ? $row['al_numimage'] . ' images' : $row['al_numimage'] . ' image';*/
    
    echo /*ECHO THE ALBUMS*/
    '<tr>'.
    '<td>'.
    '<a href="index.php?page=list-image&album=' . $row['al_id'] . '">' .
    '<img src="viewImage.php?type=album&name=' . $row['al_image'] . '" border="0">' .
    '<br>' . $row['al_name'] . '</a><br />' . $numImages .
    '</td>';
    
    /*START GETTING THE IMAGES*/
    
    $query2 = "SELECT im_id, im_title, im_thumbnail
    FROM tbl_image
    WHERE im_album_id = '$albumid'
    ORDER BY im_title";
    
    
    $result2 = mysql_query($query2) or die('Error, list image failed. ' . mysql_error());
    
    while ($row2 = mysql_fetch_assoc($result2)) {
    	echo
    	/*ECHO THE IMAGES*/
    	'<td>'.
    	'<a href="?page=image-detail&album=' . $albumId . '&image=' . $row2['im_id'] . '">' .
    	'<img src="viewImage.php?type=glthumbnail&name=' . $row2['im_thumbnail'] . '" border="0">' .
    	'</td>';
    
    }
    echo '</tr>';
    }
    ?>
    </table>
      Write a Reply...