2011-11-13 94 views
0

我需要打印多行並在每行內多個td s ...s後應該添加tr但我不確定如何處理此問題。使用php和sql值打印表格

echo '<table>'; 
    for($counter=0; $counter <= $row2 = mysql_num_rows($result2);counter++) { 
    print("<tr>"); 

    while($row2 = mysql_fetch_array($result2)) { 
     $_name = $row2["_name"]; 
     $_image = $row2["_image"]; 

     print("<td valign='top' width='230px' height='148px'>"); 
     print("<p class='p_imageStyle'>$_name</p>"); 
     print("<img class='custom' alt='' src='$_image' />"); 
     print("</td>"); 
    } 

    print("</tr>"); 
    } 

    echo '</table> '; 
+0

不要[做](http://www.hotdesign.com/seybold/everything.html),做[現代的東西(http://www.google.co。 uk/search?sourceid = chrome&ie = UTF-8&q = photo + grid + css)。 – Quentin

+0

有人發佈了一個答案,它的工作..請轉貼,以便我可以承認您的迴應...它是唯一缺少增加計數器。 – user710502

回答

2
echo '<table>'; 
    $counter=1; 
    while($row2 = mysql_fetch_array($result2)) { 
    if($counter%3==1) 
    print("<tr>"); 
    $_name = $row2["_name"]; 
    $_image = $row2["_image"]; 

    print("<td valign='top' width='230px' height='148px'>"); 
    print("<p class='p_imageStyle'>$_name</p>"); 
    print("<img class='custom' alt='' src='$_image' />"); 
    print("</td>"); 
    if($counter%3==0) 
    print("</tr>"); 
    $counter++; 
} 
$counter--; 
$rem=3-$counter%3; //modifed as per alartur 
for($i=0;$i<$rem;$i++) 
{ 
    echo "<td></td> //print remaining td 
} 
if($rem!=0) 
echo "</tr>"; //close tr if not closed 
echo '</table> '; 
+0

剩餘的tds數量是錯誤的(如果rem!= 0,則應該計算最多爲(3-rem)),但對於其餘部分似乎沒問題。 – alexpirine