user710502
user710502

Reputation: 11471

printing a table with php and sql values

I need to print several rows and within each row several tds... After 3 tds it should add a tr but I am not sure how to approach this.

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

Upvotes: 0

Views: 96

Answers (1)

Dev Jadeja
Dev Jadeja

Reputation: 130

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

Upvotes: 2

Related Questions