1

I have a text file I am reading from. The main goal is to display the information from the text file into an html table. The information for me is reading okay however all the information is being displayed into the same row.

Current code: Table Headers

        <tr>
            <th>Title</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Date of Birth</th>
            <th>Social Number</th>
            <th>Address</th>
            <th>Email</th>
            <th>Phone Number</th>
        </tr>

<?php

    if(!file_exists("patientdata.txt"))
    {
        echo "The file from above cannot be found!";
        exit;
    }
    
    $fp = fopen("patientdata.txt", "r");
    
    if(!$fp)
    {
        echo "File cannot be opened";
        exit;
    }
    echo "<table border = 3>";
    
    while(!feof($fp))
        {
            $info = fgets($fp);
            echo "<td>$info</td>\n";
        }
        echo "</td>\n";
        echo "</table>";
    fclose($fp)
?>
</body>
3
  • Use file_get_contents() Commented Oct 12, 2020 at 18:19
  • 1
    How is the data in the text file delimited? Commented Oct 12, 2020 at 18:23
  • The contents of the file is saved line by line, so each variable entered is saved to a new line. EG Miss Chole Roberts 18-02-2020 9082179041 etc Commented Oct 12, 2020 at 18:27

1 Answer 1

1

You could use following to print the data from your text file in a HTML table:

Notes:

The <style>...</style> is a small in-line stylesheet, which allows you to style your table, and other HTML elements in your page (you can also create a separate css file with all your document styling and include it in the <head>...</head> of your HTML document).

To create a HTML table from your data, you need to make sure you don't exceed the number of columns (8 in this case), so we set $cols to 8 and use a counter ($count) to keep track.

So, when 8 columns are filled with data, a new row is started. The logic keeps rendering rows, until there's no more data left, and the table is closed </table>.

<?php
if (!file_exists("patientdata.txt")) {
    echo "The file from above cannot be found!";
    exit;
}

$fp = fopen("patientdata.txt", "r");

if (!$fp) {
    echo "File cannot be opened";
    exit;
}

// a bit of styling...
echo <<<EOF
<style>
table, td, th {
  table-layout: fixed;
  width: 100%;
  border-collapse: collapse;
  border: 3px solid black;
  text-align: left;
}
</style>
EOF;

$count = 0;
$cols = 8; // the number of data items per row
echo '<table>'; // open table
// render headers
echo '<tr><th>Title</th><th>First Name</th><th>Last Name</th>
        <th>Date of Birth</th><th>Social Number</th><th>Address</th>
        <th>Email</th><th>Phone Number</th></tr>';
echo '<tr>'; // open first row
while(!feof($fp))
{
    if($count < $cols) {
        $info = fgets($fp);
        echo "<td>$info</td>"; // render data item
        $count++;
    } else {
        $count = 0; // reset counter
        echo '</tr><tr>'; // close current row, start new row
    }
}
echo "</tr></table>"; // close final row, close table
fclose($fp); // close file handle

Output: enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Glad I could help!
@MarcShields if this answers your query, you should tick the button to accept the answer and also consider upvoting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.