Trouble parsing a text file.

Discussion in 'PHP' started by mgrohan, Oct 11, 2008.

  1. #1
    I'm having trouble parsing a text file, and putting the information in table format.

    The txt file i am parsing is in the format:
    name,dname,date
    name,dname,date
    name,dname,date

    Have searched all night for code that may help, and the closest i could find was:
    
    $fichier = 'textfil.txt';
    if($fp = fopen($fichier, 'r')) {
    $ligne = 1;
    echo '<table border="1" bordercolor="#00CCFF" width="500">';
    echo '<tr align="center"><td colspan="3">Title</td>';
    while (!feof($fp)) {
    
    $data = fgets($fp);
    list ($name, $dname) = explode(',', $data);
    list ($tow, $obj) = explode(',', $dname);
    echo '<tr>';
    echo '<td>', $name, '</td>';
    echo '<td>', $tow, '</td>';
    echo '<td>', $obj, '</td>';
    echo '</tr>';
    $ligne++;
    }
    
    echo '</table>';
    
    fclose($fp);
    } else {
    exit('Error : open impossible ' . $fichier);
    }
    
    PHP:
    However it is not working.

    Any help greatly appreciated.
     
    mgrohan, Oct 11, 2008 IP
  2. djzmo

    djzmo Active Member

    Messages:
    165
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #2
    $the_file = "yourfile.txt";
    $file_content = @file_get_contents($the_file);
    $file_content_exp = explode('\n', $file_content);
    foreach($file_content_exp as $key => $value)
    {
     $data_exp = explode(',', $value);
     $name[$key] = $data_exp[0];
     $dname[$key] = $data_exp[1];
     $date[$key] = $data_exp[2];
    }
    
    PHP:
    now you can call the variable $name, $dname, and $date.

    print $name[0];
    PHP:
    the code above prints the name of the very first line of the file.

    If you want to print the datas in a table like the code you posted,
    $table = '<table border="1" bordercolor="#00CCFF" width="500">';
    $table .= '<tr align="center"><td>Name</td><td>DName</td><td>Date</td></tr>';
    for($i=0; $i<=count($name); $i++)
    {
     $table .= '<tr><td>' . $name[$i] . '</td><td>' . $dname[$i] . '</td><td>' . $date[$i] . '</td></tr>';
    }
    $table .= '</table>';
    print $table;
    PHP:
     
    djzmo, Oct 11, 2008 IP
    mgrohan likes this.
  3. mgrohan

    mgrohan Active Member

    Messages:
    671
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Thanks for the help djzmo, I have tried to code to print the data in the tables, but it is is only displaying 1 row of data. And finishes too earlier, not returning the rest?
     
    mgrohan, Oct 11, 2008 IP
  4. djzmo

    djzmo Active Member

    Messages:
    165
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Make sure that you are printing the data in a loop like this one:

    $table = '<table border="1" bordercolor="#00CCFF" width="500">';
    $table .= '<tr align="center"><td>Name</td><td>DName</td><td>Date</td></tr>';
    for($i=0; $i<=count($name); $i++)
    {
     $table .= '<tr><td>' . $name[$i] . '</td><td>' . $dname[$i] . '</td><td>' . $date[$i] . '</td></tr>';
    }
    $table .= '</table>';
    print $table;
    PHP:
     
    djzmo, Oct 11, 2008 IP