Paul Paul - 2 months ago 17
MySQL Question

PHP Table data output isn't retaining structure

I have a mySQL database, and I'm using PHP to grab it and display it in a HTML table. It's set up like this:

<table>
<tr>
<td>Title 1</td>
<td>Title 2</td>
</tr>

<?php

$i=0;
while ($i < $num) {

$col1=mysql_result($result,$i,"col1");
$col2=mysql_result($result,$i,"col2");

?>

<tr>
<td><?php echo $col1; ?></td>
<td><?php echo $col2; ?></td>
</tr>
</table>

<?php

$i++;
echo '<br />';
}

?>


The problem: for the first set of data it pulls, the columns align with the titles. But for every repeating row after that, the columns are separated by one space, and don't align with their titles. It looks like this:

Title 1 Title 2

col1 col2

col1 col2
col1 col2


Sorry if the wording is confusing.

Answer Source

You're closing the table inside the loop, try to pull the </table> outside of the loop.