I have built a cool script which opens a CSV and outputs the data into an HTML table - neat! :-)
However, I was wondering, is it possible to take the first row of data (table headings) and put these inside a thead and th elements?
<?php
echo "<table class='table table-bordered'>\n\n";
$f = fopen("users.csv", "r");
while (($line = fgetcsv($f)) !== false) {
$row = "<tr>";
$is_empty = false;
foreach ($line as $cell) {
if ($cell !== '') {
$row .= "<td>" . htmlspecialchars($cell) . "</td>";
} else {
$is_empty = true;
}
}
$row .= "</tr>\n";
if ($is_empty) {
continue;
} else {
echo $row;
}
}
fclose($f);
echo "\n</table>";
?>
Right now my HTML is:
<table class='table table-bordered'>
<tr><td>Forename</td><td>Surname</td><td>Extension</td></tr>
<tr><td>Jim</td><td>Carey</td><td>9843</td></tr>
</table>
Can I change this to:
<table class='table table-bordered'>
<thead><tr><th>Forename</th><th>Surname</th><th>Extension</th></tr></thead>
<tr><td>Jim</td><td>Carey</td><td>9843</td></tr>
</table>
Thank you for any guidance.