2016-10-25 87 views
-1

我已經完成上傳文件到服務器和mysql數據庫,即時通訊顯示圖像從我的表上的數據庫當有人上傳圖像我想有最多3列和第四圖像我想顯示在第一列和第二排等,所以我需要寫循環我認爲,但不知道如何做到這一點。PHP循環表

$query = mysql_query($sql) or die(mysql_error()); 

echo "<table>"; 
echo "<tr>"; 
while($row = mysql_fetch_array($query)) 
{ 
    $link = $row['link']; 
    $catg = $row['kategoria']; 
    $comm = $row['komentarz']; 

    switch($catg){ 
     case "standing": 
      echo "<td>";?> <img src="img/<?php echo $catg; ?>/<?php echo $link; ?>" width="150" height="150"> <?php echo "</td>"; 
    } 
} 
echo "</tr>"; 
echo "</table>"; 
+0

'如果計數器等於3 - 啓動新row' –

+0

但我真的不能做這個 – lethzik

+0

循環真的不能做什麼 - 我+ +'$'? '$ I/3'? –

回答

0

使用MOD師:

$query = mysql_query($sql) or die(mysql_error()); 

echo "<table>"; 
echo "<tr>"; 

$c=0; 
while($row = mysql_fetch_array($query)) 
{ 
    $c++; 
    $link = $row['link']; 
    $catg = $row['kategoria']; 
    $comm = $row['komentarz']; 

    switch($catg){ 
     case "standing": 
      echo "<td>";?> <img src="img/<?php echo $catg; ?>/<?php echo $link; ?>" width="150" height="150"> <?php echo "</td>"; 
    } 

    if ($c % 3 == 0) echo "</tr><tr>"; 
} 
echo "</tr>"; 
echo "</table>";