Jump to content

php array table of images


smonkcaptain

Recommended Posts

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

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>';
?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.