Results 1 to 6 of 6

Thread: Displaying data from a textfile, having a lot of difficulties, help needed!!!

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Displaying data from a textfile, having a lot of difficulties, help needed!!!

    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:

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

  2. #2
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    PHP Code:
    <?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>";
    ?>

  3. #3
    Join Date
    Sep 2006
    Location
    Eureka, California
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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($line1) : $line); 
        
    //
        // remove null values from the end for table beautification purposes
        //
        
    $line = (substr($line, -11) == '|' substr($line0, -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.

  4. #4
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    I'd say that my code is still somehow better, the only thing that needs to be changed is:
    Code:
    <?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

  5. #5
    Join Date
    Sep 2006
    Location
    Eureka, California
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  6. #6
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks, guyz!!! This is pretty much what I want but forgive me if I need to bother you again.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •