2011-04-02 56 views
0

在一個數據庫中有一些圖像[未知數],我需要顯示他們3連續 這裏是我的代碼,但它不工作[在某一行它顯示2或4張照片] :(顯示3張圖像在一張表中的10張圖像或更多的列表 - PHP

<?php 
$temp=0; 
$stmt = mysqli_prepare($con,"select PicName from Pic_Details"); 
mysqli_stmt_execute($stmt); 
mysqli_stmt_bind_result($stmt,$picName); 
while(mysqli_stmt_fetch($stmt)) 
{ 
if ($temp % 3==0) 
    echo '<tr>'; 

echo '<td valign="top" width="150px"><img src="'.$picName.'"/></td>'; 

if ($temp % 3==0) 
    echo '</tr>'; 
++$temp; 
} 

?> 

回答

0

請嘗試以下... ofcourse,如果您的圖片總數不能被三整除,您需要在某些邏輯中工作來檢測該圖片,並在最後一行添加3個單元格。

<?php 
$temp=0; 
$stmt = mysqli_prepare($con,"select PicName from Pic_Details"); 
mysqli_stmt_execute($stmt); 
mysqli_stmt_bind_result($stmt,$picName); 
while(mysqli_stmt_fetch($stmt)) 
{ 
if ($temp % 3==0) 
    echo '<tr>'; 

echo '<td valign="top" width="150px"><img src="'.$picName.'"/></td>'; 

if ($temp % 3==2) // remainder should be 2 on the last cell 
    echo '</tr>'; 
++$temp; 
} 

?> 
0

我只是覺得你有問題,您的條件語句。

我會在這裏測試一下,並清理代碼有點:

if ($stmt = mysqli_prepare($con,"select PicName from Pic_Details")) { 

    echo "<table>"; 
    while(mysqli_stmt_fetch($stmt)) { 
     echo ($temp % 3 != 0) ? '' : '<tr>'; 
     echo '<td valign="top" width="150px"><img src="{$picName}"/></td>'; 
     echo ($temp % 3 != 0) ? '' : '</tr>'; 
    ++$temp; 
    } 
    echo "</table>"; 

} else { 
    echo "failed at: mysqli_prepare"; 
} 
相關問題