Page 1 of 1

PHP Gallery problem... RESOLVED

Posted: Thu Jul 10, 2008 1:15 pm
by onoffpaul
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:

Code: Select all

 
<table border="0" cellpadding="5" cellspacing="0">
 
<?  
$db_cnx = mysql_connect ('localhost', 'onoff', 'hello');
$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)) {
                                        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>
 
 

Re: PHP Gallery problem... HELP URGENTLY NEEDED!

Posted: Thu Jul 10, 2008 1:28 pm
by WebbieDave
Saying "urgent" and using CAPS certainly won't get your post answered any faster :) In fact, it may even delay response! Please review the posting guidelines and then reply with a more readable post:

viewtopic.php?f=1&t=8815

Thanks!

Re: PHP Gallery problem... help needed!

Posted: Thu Jul 10, 2008 2:30 pm
by WebbieDave
Thanks for taking the caps out. Can you properly indent your code and place it within php tags (see BBCode)? That will make things much clearer and easier to read (see posting guidelines link in previous post for more info).

Re: PHP Gallery problem... help needed!

Posted: Thu Jul 10, 2008 7:13 pm
by Stryks
In answer to your problem, you actually seem to have designed it to do what it is doing.

You go out of your way to select the last album id ...

Code: Select all

while ($get_id = mysql_fetch_assoc($result)) { $albumid = $get_id['al_id'];}
And then you use that ID to return the images ...

Code: Select all

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

Code: Select all

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

Code: Select all

$query2 = "SELECT im_id, im_title, im_thumbnail
FROM tbl_image
WHERE im_album_id = '{$row['al_id']}'
ORDER BY im_title";
... might be along the right lines.

See how much nicer that code looks when it's tagged properly. We're all happy to help, but people wont usually wade through code in plain text to help you.

Next time, use the code tags and help us help you.

Cheers

Re: PHP Gallery problem... help needed!

Posted: Fri Jul 11, 2008 3:52 am
by onoffpaul
Hey,

Thanks a mil... that worked perfectly!!!

Cheers,

Paul