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. Quote Link to comment 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>'; ?> Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.