On Wed, 14 Feb 2007 19:27:56 +0100, Richard wrote:
>
> 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?
HTML table or database table? If HTML:
1. If column count is absolutely consistent:
<?php
echo '<table>;
$handle = fopen('/path/to/text/file');
while($array = fgetcsv($handle)){
echo '<tr><td>';
echo implode('</td><td>',$array);
echo '</td><tr>';
}
echo '</table>;
?>
2. If columncount not consistent or unsure:
<?php
$handle = fopen('/path/to/text/file');
$columncount = 0;
$rows = array();
while($array = fgetcsv($handle)){
$rows[] = $array;
$columncount = (count($array) > $columncount) ? count($array) :
$columncount;
}
$rowstring = '<tr>'.str_repeat('<td>%s</td>',$columncount).'</tr>';
echo '<table>';
foreach($rows as $row){
vprintf($rowstring,array_pad($row,$columncount,'');
}
echo '</table>';
?>
--
Rik Wasmus
>> Stay informed about: build table from external text source