Announcement

Collapse
No announcement yet.

how to read txt file?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to read txt file?

    i wanna make a table using php, and the php file reads from a txt file to get the data. just like using a DB. can someone give me an example?

    maybe use commas to seperate columns and line breaks to seperate rows.

    thanks

  • #2
    There you go. Please note that this is a quick and basic solution. It'll do the job, but there's no error checking in terms of "do all lines have the same amount of columns" etc. Hope this helps.

    Code:
    $datafile = "path/to/datafile.txt"; // columns in every line should be separated by "||||"
    
    $rawfile = file($datafile);  // file is read into array $rawfile
    
    echo "<table>\n";
    
    for ($a = 0; $a < count($rawfile); $a++) {
        echo "<tr>\n";
        $columns = explode("||||", $rawfile[$a]);
    
        for ($b = 0; $b < count($columns); $b++) {
            echo "<td>".$columns[$b]."</td>\n";
        }
    
        echo "</tr>\n";
    }
    
    echo "</table>\n";

    Comment


    • #3
      Thanks Gossip youre the BEST!

      Comment

      widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
      Working...
      X