table appears with empty rows, and row data appears before table. Code seems ok. What is going on?



I have code which displays a HTML <table>. The data in the rows comes is read from a file. But the row data is appearing (minus the <tr></tr> tags) on the web page before the table. The table displays with empty rows below what was supposed to be the row data.

In other words this php...

echo "<table>\n<thead>\n<tr><th>number</th><th>date</th><th>First Name</th><th>Last Name</th><th>state</th><th>country</th><th>remarks</th></tr>\n</thead>\n<tbody>\n";

try {

$fileh = fopen("data/petition-051512.csv", "r");

while ($text = fgets($fileh)) {

$tok = strtok($text, ",");

while ($tok !== FALSE) {

echo "<tr>" . $tok . "</tr>\n";

$tok = strtok(",");
}
}
echo "</tbody></table>\n";
return;

produces html that looks like one big mass of text, consisting of the 560 rows of data, minus any table tags, followed by a table with 560 empty rows.

I can't figure this out.
.