2011-10-26 41 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>'; 
?>