tags:

views:

183

answers:

3

Hello,

I want to show nine images in a table , three images in each row using php.

+1  A: 

It mostly depends on how you have PHP representing the images. The HTML markup should look something like

<div class="img-table">
    <img ...>
    <img ...>
    <img ...>
    ....
</div>

and the CSS:

.img-table {
    width:1000px; /* or whatever works */
}
.img-table img {
    width:30%;
    height:auto;
    display:inline;
    margin: 10px 1.5%; /* spacing: 20px vertically, 5% horizontally */
}

or, if you want to use <table> tables:

<table class="img-table">
    <tr>
        <td><img ...></td>
        <td><img ...></td>
        <td><img ...></td>
    </tr>
    .....
</table>

If you have your image urls located in a array, like

$imgs = array('path/to/img1.jpg','path/to/img2.jpg',...);

then you can generate the HTML like this:

$index = 0;
$html = '<div class="img-table">';
for ($i=0; $i<3; $i++) {
    for ($j=0; $j<3; $j++) {
        $html .= '<img src="' . $imgs[$index++] . '">';
    }
}
$html .= '</div>';
echo $html;

or for the table:

$index = 0;
$html = '<table class="img-table">';
for ($i=0; $i<3; $i++) {
    $html .= '<tr>'
    for ($j=0; $j<3; $j++) {
        $html .= '<td>';
        $html .= '<img src="' . $imgs[$index++] . '">';
        $html .= '</td>';
    }
}
$html .= '</table>';
echo $html;

Hope that helps.

Austin Hyde
+1  A: 

Laying out column images in a table

<? $perrow=3; $n=0; ?>
<table>
<? foreach($images as $i): ?>
    <? if($n == 0): print '<tr>'; endif; ?>
    <td><img src="<?=$i?>"></td>
    <? if(++$n >= $perrow): print '</tr>'; $n = 0; endif; ?>
<? endforeach; ?>
</table>
Rob
A: 
<?php
      $images=ARRAY( a.gif","b.jpeg","c.jif","d.bmp");    
?>

<table border="0" cellspacing="1" cellpadding="0">
    <tr>
      <?php
          for($i=0; $i< count($images); $i++) {
             echo "<td>
                   <table width=100% border=0>
                      <tr><td>
  <img src=Download/$images[$i] width=130 height=130 border=0></td></tr>
                   </table>
                   </td>";
  if ( (($i+1) % 3) == 0 ) echo $newrow="</tr><tr>"; // change 6 to 8 and see
}
    ?>                
</tr>
</table>
Muhammad Sajid