2011-10-26 33 views
0

我需要在表格中显示一个相册,我想在每一行显示五张图片,但找不到在每张图片后面插入</tr><tr>的方法。 这里是我的代码:html表格显示php相册

<?php 

// table name 
$tbl_name=gallery1; 
$sql="SELECT * FROM $tbl_name"; 
$result=mysql_query($sql); 

while($rows= mysql_fetch_assoc($result)){ 

$id   = $rows['id']; 
$path  = $rows['path']; 
$image_name = $rows['image_name']; 
$title  = $rows['title']; 
?> 


<img src="<?php echo $path."/".$image_name;?>" height="120"/>Name:<?php echo $title;?> 
<?php 
echo "<form action='pictry.php' enctype='multipart/form-data' method='post'> 
<input name='file[]' type='hidden' value='".$image_name."' /> 
<input name='id' type='hidden' id='id' value='".$id."'/> 
<input type='submit' name='button' id='button' value='Delete Picture' /></form>"; 

} 
?> 

回答

1

替换行

while($rows= mysql_fetch_assoc($result)){ 

for($i = 0; $rows= mysql_fetch_assoc($result); ++$i) { 

然后你把这样的事情到for循环。

if($i % 5 == 0) { /* insert stuff */ } 
+0

很好,谢谢! – Tamara

0

未经测试的代码

<?php 
    $perrow = 5; 
    $i = 0; 
    echo '<table>'; 
    echo '<tr>'; 
    while($rows = mysql_fetch_assoc($result)) { 
    echo '<td><img src=[grapresultfromrows] /></td>'; 
    ++$i; 
    if($i == $perrow) { 
     $i = 0; 
     echo '</tr>'; 
     echo '<tr>'; 
    } 
    } 
    // If not a multiple of $perrow you need to add extra cells 
    for($i; $i < $perrow; ++$i) { 
    echo '<td></td>'; 
    } 
    echo '</tr>'; 
    echo '</table>'; 
?>