build table from external text source

I am completely new to PHP.

I am looking for a code snippet which would take as argument an external
text file which concists of multiple lines of 2 columns with each column
in quotes or somesuch and then generate in place a table holding that
information.

Any pointers or ideas?
Richard [ Mi, 14 Februar 2007 19:27 ] [ ID #1629297 ]

Re: build table from external text source

On Wed, 14 Feb 2007 19:27:56 +0100, Richard <rgrdev [at] 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
Rik [ Mi, 14 Februar 2007 19:48 ] [ ID #1629299 ]
PHP » alt.php » build table from external text source

Vorheriges Thema: How to ensure that EMAIL from mail() do not goes into SPAM folder?
Nächstes Thema: PHP testing