erieDesign Posted March 8, 2010 Share Posted March 8, 2010 Hello, I need some help with a while loop that is displaying images in a photo gallery from a MySQL database. I am pretty amateur with php and had some assistance on this code. In anycase It is supposed to display 4 images from my database, then once complete create another row and display the next 4 from the database and so on until no more images to display. Right now., it displays the first row correctly showing 4 images, but THEN displays a 2nd row displaying the remaining 15 images. I don't know why it does not keep displaying the rows with 4 images. Can anyone see anything wrong with this code: Thanks for any assistance. <? $pQ = mysql_query("SELECT * FROM `galleryPhotos` WHERE `gallery` = '".$gallery['id']."' ORDER BY `id` ASC"); $c = 1; $url = returnURL(); if(mysql_num_rows($pQ) > 0){ while($photo = mysql_fetch_array($pQ)){ ?> <? if($c == 1){ echo "<tr>"; } ?> <? echo "<td class=\"gallery_photo\"><a href=\"".$url."/photos/1/".$photo['photo']."\" title=\"".$photo['caption']."\" rel=\"lightbox[gallery]\"><img src=\"".$url."/photos/s/".$photo['photo']."\" border=\"1\"></a></td>"; ?> <? if($c == 4){ echo "</tr>"; } ?> <? $c++; } if($c > 1){ for($i = $c; $i <= 4 ; $i++){ echo "<td class=\"gallery_photo\"> </td>"; } echo "</tr>"; } } else { ?> <? } ?> Link to comment https://forums.phpfreaks.com/topic/194510-loop-not-displaying-images-correctly/ Share on other sites More sharing options...
gwolgamott Posted March 8, 2010 Share Posted March 8, 2010 Add this after the $c++; in the while loop if($c == 5) {$c = 1;} See the comments in the code below and see if that works for you. <?php $pQ = mysql_query("SELECT * FROM `galleryPhotos` WHERE `gallery` = '".$gallery['id']."' ORDER BY `id` ASC"); $c = 1; $url = returnURL(); if(mysql_num_rows($pQ) > 0) { while($photo = mysql_fetch_array($pQ)) { if($c == 1){ echo "<tr>"; } echo "<td class=\"gallery_photo\"><a href=\"".$url."/photos/1/".$photo['photo']."\" title=\"".$photo['caption']."\" rel=\"lightbox[gallery]\"><img src=\"".$url."/photos/s/".$photo['photo']."\" border=\"1\"></a></td>"; if($c == 4){ echo "</tr>"; } $c++; /////////added this, you were not resetting $c so the tr tags were not being displayed again after the first 4//////// if($c == 5) {$c = 1;} /////////added code/////////// } if($c > 1) { for($i = $c; $i <= 4 ; $i++) { echo "<td class=\"gallery_photo\"> </td>"; } echo "</tr>"; } } else { } ?> Link to comment https://forums.phpfreaks.com/topic/194510-loop-not-displaying-images-correctly/#findComment-1023046 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.