Log in

View Full Version : Displaying data from a textfile, having a lot of difficulties, help needed!!!



shachi
10-15-2006, 11:34 AM
Hello all,

Can anyone help me displaying data in a textfile edited by the user. What I mean is if the user edits his/her file like:



data1|data2|data3
data4|data5|data6
...


Now I want it to be rendered as a table in which each pipe character represents a column and each new line represents a row. The problem I am having is that I cannot make a dynamic table for it as every user doesn't enter the same number of columns and rows.

Any help would be greatly appreciated, if you need more information on this subject then I would be happy to give.

Thanks again.

DimX
10-15-2006, 12:18 PM
<?php
$fc = file("the_file.txt");
echo "<table border='1'>\n";
foreach($fc as $line) {
echo "<tr>\n";
$tds = explode("|", trim($line));
foreach($tds as $td) {
echo "<td>$td</td>\n";
}
echo "</tr>\n";
}
echo "</table>";
?>

AbelaJohnB
10-15-2006, 11:27 PM
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:



|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:



$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 : '&nbsp;') . '</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.

DimX
10-16-2006, 02:07 PM
I'd say that my code is still somehow better, the only thing that needs to be changed is:


<?php
$fc = file("the_file.txt");
echo "<table border='1'>\n";
foreach($fc as $line) {
echo "<tr>\n";
$tds = explode("|", trim($line));
foreach($tds as $td) {
echo "<td>".($td ? str_replace(" ", "&nbsp;", $td) : "&nbsp;")."</td>\n";
}
echo "</tr>\n";
}
echo "</table>";
?>

Here's a comparison of our codes: http://roboweb.awardspace.com/file_table.php

AbelaJohnB
10-16-2006, 06:45 PM
Brillent ;)

Yet another case of overthinking things.

HOPEFULLY though (lol) somebody would never allow their flat-file to become that physco!


none-the-less.... if this is for data storage... still gotta have a primary key in there folks ;)

shachi
11-12-2006, 03:42 PM
Thanks, guyz!!! This is pretty much what I want but forgive me if I need to bother you again. :p