note 93962 deleted from function.fgetcsv by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 11:17:21 +0000 |
| Subject: | note 93962 deleted from function.fgetcsv by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197077@lists.php.net to get a copy of this message | ||
Note Submitter: rt10k at yahoo dot com
----
build a basic html table from a csv file
<style>
table { text-align: left; border-collapse: collapse; }
tr:hover { background: blue; color: white }
th, td { padding: 7px }
</style>
<?php
echo "<table>\n";
$row = 0;
$handle = fopen("mycsvfile.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($row == 0) {
// this is the first line of the csv file
// it usually contains titles of columns
$num = count($data);
echo "<thead>\n<tr>";
$row++;
for ($c=0; $c < $num; $c++) {
echo "<th>" . $data[$c] . "</th>";
}
echo "</tr>\n</thead>\n\n<tbody>";
} else {
// this handles the rest of the lines of the csv file
$num = count($data);
echo "<tr>";
$row++;
for ($c=0; $c < $num; $c++) {
echo "<td>" . $data[$c] . "</td>";
}
echo "</tr>\n";
}
}
fclose($handle);
echo "</tbody>\n</table>";
?>