smonkcaptain Posted July 24, 2010 Share Posted July 24, 2010 Hello all. http://www.aviation-photographs.net/ On my website's left hand side you can see 4 photographs. I'm wanting to show these pictures in a 2x2 table however i'm not sure how to do it! Here is my current code: <?php $limit=4; $latest=mysql_query("select photoname from `queued_photos` LIMIT $limit"); while($thumbs=mysql_fetch_array($latest)){ $photoname=($thumbs['photoname']); echo ' <table width="150" style="border: 1px solid black;" cellpadding="0" cellspacing="0"> <tr> <td><img src="../queued/'.$photoname.'_thumb.jpg"></td> </tr> </table> '; } ?> I've tried to echo each one in the <td> tags, however, then i get 4 tables (because the limit is 4)! Thanks all, Jimmy. Link to comment https://forums.phpfreaks.com/topic/208771-php-array-table-of-images/ Share on other sites More sharing options...
Pikachu2000 Posted July 24, 2010 Share Posted July 24, 2010 You can use the modulus operator to see when to end and reopen the <tr> tags. This will keep the images 2 across regardless of the number of images the query limited to, and sill complete the last table row if it's an odd number, so the table isn't "broken". <?php $limit=4; //$latest=mysql_query("select photoname from `queued_photos` LIMIT $limit"); $i = 1; echo "<table width=\"150\" style=\"border: 1px solid black;\" cellpadding=\"0\" cellspacing=\"0\">\n"; while($i < { echo $i % 2 == 0 ? '' : "<tr>\n"; echo "<td><img src=\"../queued/{$thumbs['photoname']}_thumb.jpg\"></td>\n"; echo $i % 2 != 0 ? '' : "</tr>\n"; $i++; } echo $i % 2 != 0 ? '' : "<td> </td>\n</tr>\n"; echo '</table>'; ?> Link to comment https://forums.phpfreaks.com/topic/208771-php-array-table-of-images/#findComment-1090653 Share on other sites More sharing options...
smonkcaptain Posted July 24, 2010 Author Share Posted July 24, 2010 Thanks for the reply, is there anyway to add spacing between each image, and have a border around each image? Link to comment https://forums.phpfreaks.com/topic/208771-php-array-table-of-images/#findComment-1090704 Share on other sites More sharing options...
smonkcaptain Posted July 24, 2010 Author Share Posted July 24, 2010 Ignore my last post. I've sorted it Link to comment https://forums.phpfreaks.com/topic/208771-php-array-table-of-images/#findComment-1090705 Share on other sites More sharing options...
Pikachu2000 Posted July 24, 2010 Share Posted July 24, 2010 Just noticed I didn't edit the code back to your database image array. Did you get that figured out as well? Link to comment https://forums.phpfreaks.com/topic/208771-php-array-table-of-images/#findComment-1090710 Share on other sites More sharing options...
smonkcaptain Posted July 24, 2010 Author Share Posted July 24, 2010 I eventually gave it a crack myself and managed to sort it. Here is my code: <?php $limit=4; $latest=mysql_query("select photoname from `queued_photos` LIMIT $limit"); echo '<table width="150" cellpadding="0" cellspacing="8" style="margin-top: -20px;"> <tr>'; $i = 1; while ($thumbs = mysql_fetch_array($latest)) { $photoname = ($thumbs['photoname']); echo '<td><center><img src="../queued/' . $photoname . '_thumb.jpg" style="border: 1px solid black"></center></td>'; if ($i % 2 == 0) { echo '</tr><tr>'; } $i++; } echo '</tr></table>'; ?> and here is the link so you can see the outcome: www.aviation-photographs.net If you see anything wrong with my coding though, let me know! Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/208771-php-array-table-of-images/#findComment-1090762 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.