Re: best way to turn a text file into tables in PHP

From: Date: Mon, 09 Sep 2002 06:51:03 +0000
Subject: Re: best way to turn a text file into tables in PHP
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-115706@lists.php.net to get a copy of this message
So, you have a text file with tab delimited values, one row per line yes? --- <TABLE> <? $file = 'path/to/your/file.txt'; $fp = fopen ($file,"r"); $row = 0; while ($myrow = fgetcsv ($fp, 1000, "\t")) { echo "<TR>"; foreach($myrow as $k => $v) { if($row == 0) { $v = "<B>{$v}</B>"; } echo "<TD>{$v}</TD>"; } echo "</TR>\n"; $row++; } fclose ($fp); ?> </TABLE> --- Notes: 1. untested code, but should be fine 2. if you're not using a tab to separate your column values, it won't work 3. I'm wrapping the first rows values in <B>tags</B> to give some sort of heading, hence the need for the $row++ stuff... 4. for more information on it all, check out all the functions used in the manual, in particular fgetcsv() Enjoy! Justin French on 09/09/02 4:29 PM, Peter (rpete@hotmail.com) wrote: > Hi, > I have 'test_file.txt' with the contents: > > label1 label2 label3 comments > name1 name2 name3 > name4 name5 name6 This is a comment > ... > > How can I turn this into a table using PHP? > > > Thanks, > -Peter > >

« previous php.general (#115706) next »