用php和sql值打印一个表

时间:2011-11-13 22:46:34

标签: php mysql

我需要打印多行,并且在每行内打几个td s ...在3 td之后,它应该添加tr,但我不知道如何处理此问题。

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> ';

1 个答案:

答案 0 :(得分: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> ';
相关问题