Hello,
The above code is a great start... but it does not deal with a lot of issues you will probably encounter along the way.
For example, let's look at this example flat-file:
Code:
|data1|data2|data3
data4||data6|
The above code does not properly handle this...
So, let's expand on the code a few lines, and end up with this:
PHP Code:
$fc = file('/path_to/the_file.txt');
echo '<table border="1">';
foreach($fc as $line)
{
//
// first array value should ALWAYS be a unique key... if it is null, remove it!!
// (you could also not remove it and instead add a primary key)
//
$line = ($line{0} == '|' ? substr($line, 1) : $line);
//
// remove null values from the end for table beautification purposes
//
$line = (substr($line, -1, 1) == '|' ? substr($line, 0, -1) : $line);
//
// explode everything...
//
$tds = explode('|', trim($line));
//
// all that done, output data...
//
echo '<tr>';
foreach($tds as $td)
{
// let's make sure null td's have a value...
//
echo '<td>' . ($td ? $td : ' ') . '</td>';
}
echo '</tr>';
//
// hey, wasn't that fun!
//
}
echo '</table>';
What we have now done is produce a proper table that handles both null (arrays) values and it cleans the first/last character, should it/they end up as a pipe.
Try both of these codes (mine and the code DimX provided) using the flat-file example just above and you will see a significant difference.
Hope this helps you solve some issues.
Bookmarks