0

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> ';
2
  • Don't do that, do something modern instead. Commented Nov 13, 2011 at 22:52
  • someone posted an answer and it worked.. please repost so that i can give acknowledge your response... The only thing it was missing to increment the counter. Commented Nov 13, 2011 at 23:25

1 Answer 1

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> ';
Sign up to request clarification or add additional context in comments.

1 Comment

The number of remaining tds is wrong (you should rather count up to (3-rem) if rem != 0), but seems ok for the rest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.