Thread: txt file

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    77
    Rep Power
    15
    Hi,

    I want to open a text file and print it to a page. It first has to...

    1. split the file by line...
    2. split each line (by tabs)

    I then want to put the values into a table in the format of...
    <table>
    <tr>(line one)
    <td>element one</td>
    <td>element two</td>
    etc..
    </tr> (end line one)
    <tr>(line two)
    etc
    etc
    </table>
    can anyone help me out...
    so far I have this..

    <?
    $fp = fopen("filename.txt", "r");
    $contents = fread($fp, filesize("filename.txt"));
    fclose($fp);

    $newArray=explode("t", $contents);
    while (list($key,$value) = each($newArray)){
    echo "<td>$value</td>";
    }

    $numlines=count($newArray);
    echo "<br>Count is:$numlines<br>";
    $n=0;
    $elementArray=array();
    for ($i=0;$i<$numlines;$i++)
    {
    $elementArray[$n]=split(",",$newArray[$i]);
    $n++;
    //echo"First Element:$elementArray[$i][$n]<br>";
    }
    ?>

    the above code only splits file based on tabs... i couldn't get it to do both.

    thanks in advance.

    dave
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    15
    //Put each line into an array
    $file = file("yourfile");
    print '<table>';
    while (list($key,$val) = each($file)) {
    $str = explode("t", $val);
    print '<tr>';
    print '<td>'.$str[0].'</td>'
    print '<td>'.$str[1].'</td>'
    print '</tr>';
    }
    print '</table>';

Similar Threads

  1. Pointer problem in opening a file
    By LAKI in forum C Programming
    Replies: 13
    Last Post: February 11th, 2004, 08:27 AM
  2. Pointer problems opening file *formatted*
    By LAKI in forum C Programming
    Replies: 4
    Last Post: February 11th, 2004, 06:06 AM
  3. newbie - write to txt file
    By rasoodock in forum PHP Development
    Replies: 1
    Last Post: February 4th, 2004, 09:12 AM
  4. passing variables from a txt file to flash
    By matto in forum Flash Help
    Replies: 1
    Last Post: January 20th, 2004, 11:25 AM
  5. Replies: 4
    Last Post: January 15th, 2004, 06:14 PM

IMN logo majestic logo threadwatch logo seochat tools logo