|
Posted by Rik on 02/14/07 18:48
On Wed, 14 Feb 2007 19:27:56 +0100, Richard <rgrdev@gmail.com> wrote:
>
> I am completely new to PHP.
>
> I am looking for a code snippet which would take as argument an extern=
al
> text file which concists of multiple lines of 2 columns with each colu=
mn
> in quotes or somesuch and then generate in place a table holding that
> information.
>
> Any pointers or ideas?
HTML table or database table? If HTML:
1. If column count is absolutely consistent:
<?php
echo '<table>;
$handle =3D fopen('/path/to/text/file');
while($array =3D fgetcsv($handle)){
echo '<tr><td>';
echo implode('</td><td>',$array);
echo '</td><tr>';
}
echo '</table>;
?>
2. If columncount not consistent or unsure:
<?php
$handle =3D fopen('/path/to/text/file');
$columncount =3D 0;
$rows =3D array();
while($array =3D fgetcsv($handle)){
$rows[] =3D $array;
$columncount =3D (count($array) > $columncount) ? count($array) : =
$columncount;
}
$rowstring =3D '<tr>'.str_repeat('<td>%s</td>',$columncount).'</tr>';
echo '<table>';
foreach($rows as $row){
vprintf($rowstring,array_pad($row,$columncount,'');
}
echo '</table>';
?>
-- =
Rik Wasmus
Navigation:
[Reply to this message]
|