note 93962 added to function.fgetcsv
| From: | rt10k at yahoo dot com | Date: | Thu, 08 Oct 2009 18:23:40 +0000 |
| Subject: | note 93962 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161523@lists.php.net to get a copy of this message | ||
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>";
?>
----
Server IP: 64.71.164.2
Probable Submitter: 69.232.212.116
----
Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php
Edit -- https://master.php.net/note/edit/93962
Del: integrated -- https://master.php.net/note/delete/93962/integrated
Del: useless -- https://master.php.net/note/delete/93962/useless
Del: bad code -- https://master.php.net/note/delete/93962/bad+code
Del: spam -- https://master.php.net/note/delete/93962/spam
Del: non-english -- https://master.php.net/note/delete/93962/non-english
Del: in docs -- https://master.php.net/note/delete/93962/in+docs
Del: other reasons-- https://master.php.net/note/delete/93962
Reject -- https://master.php.net/note/reject/93962
Search -- https://master.php.net/manage/user-notes.php