Reply to Re: build table from external text source

Your name:

Reply:


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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация