2011-11-13 55 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