note 59295 added to function.fgetcsv
| From: | gibson dot a dot paulatgmail dot com at osu1 dot php dot net | Date: | Fri, 02 Dec 2005 10:14:08 +0000 |
| Subject: | note 59295 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99442@lists.php.net to get a copy of this message | ||
a simple script to output the contents of a CSV file as a nice table - should be totally dynamic and
can use any seperator - it does not have to be a comma (,).
I don't perclaim my code is 100% perfect or correct, but it works. useful if you back up your
cell phone address book and it outputs CSV.
It works on the assumtion that the first line is the header row.
<?
//Define what you want the seperator to be, this could be new line, (\n) a tab (\t) or any other
char, for obvious reasons avoid using chars that will be present in the string. Id suggest a comma,
or semicolon.
$sep = ",";
//define file to read
$file = "test.txt";
//read the file into an array
$lines = file($file);
//count the array
$numlines = count($lines);
//explode the first (0) line which will be the header line
$headers = explode($sep, $lines[0]);
//count the number of headers
$numheaders = count($headers);
$i = 0;
//start formatting output
echo "<table border = 1 cellpadding = 2><tr>";
//loop through the headers outputting them into their own <TD> cells
while($i<$numheaders){
$headers = str_replace("\"", "", $headers);
echo "<td>".$headers[$i]."</td>";
$i++;
}
echo "</tr>";
$y = 1;
//Output the data, looping through the number of lines of data and also looping through the number
of cells in each line, as this is a dynamic number the header length has to be reread.
while($y<$numlines){
$x=0;
echo "<TR>";
while($x<$numheaders){
$fields = explode($sep, $lines[$y]);
$fields = str_replace("\"", "", $fields);
echo "<TD> ".$fields[$x]." </TD>";
$x++;
}
$y++;
echo "</TR>";
}
//close the table.
echo "</table>";
?>
----
Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59295
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59295&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59295&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59295&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59295&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59295&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59295&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59295&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59295&report=yes
Search -- http://master.php.net/manage/user-notes.php