2010-01-08 61 views
1

我想在表格中显示九张图像,每行使用PHP显示三张图像。如何在一张表中显示九张图像,每行使用三个图像php

+2

你只需要CSS在这种情况下,包你的图像一个div,并给它一些宽度。然后分配每个图像约div的宽度的33%。 – 2010-01-08 03:55:57

+0

@Jay Zeng:他可能拥有一组图像的URL,并且想用PHP来生成每行3列的行。 – 2010-01-08 03:57:29

+0

我并不想冒犯我......我没有意识到你的意思是九...我将为未来的搜索者编辑这个问题。 – 2010-01-08 03:59:48

回答

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

它主要取决于你如何让PHP代表图像。在HTML标记看起来应该像

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

和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 */ 
} 

,或者,如果你想使用<table>表:

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

如果您有位于图片网址一个阵列,就像

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

那么你就可以生成这样的HTML:

$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; 

或表:

$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; 

希望有所帮助。

1

铺出列的图像在一个表中

<? $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> 
相关问题