开发者

How to show nine images in a table , three images in each row using php

I wa开发者_StackOverflownt to show nine images in a table , three images in each row using PHP.


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.


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>


<?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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜