0

I want to display a .csv file neatly in a table using php. Some content of the file are empty field.

Here is my first approach, got an error but displaying the datas in one row. Supposedly they should display in their respectively fields.

The code output here: enter image description here

Should be like this:

enter image description here

<table border = "1">
        <tr>
            <th>NAME</th>
            <th>Email</th>
            <th>Address</th>        
            <th>Payment</th>
            <th>Datepaid</th>
        </tr>
        <?php
          $data = file("data/payment.csv");
          foreach ($data as $line){
          $lineofarray = explode("\t", $line);
          list($name, $email, $address, $payment, $datepaid) = $lineofarray;//error here
        ?>
        <tr>
            <td>
                <?= $name?>
            </td>
            <td>
                <?= $email?>
            </td>
            <td>
                <?= $address?>
            </td>
            <td>
                <?= $payment?>
            </td>

            <td>
                <?= $datepaid?>
            </td>


        </tr>
        <? }?>
</table>

My code now had an error on the part of list. And displaying the datas in one field. Hope you can help me. Thanks

1

2 Answers 2

3

Please try this.

$fp = fopen("data/payment.csv", "r");
while (($line = fgetcsv($fp)) !== false) {
        echo "<tr>";
        foreach ($line as $field) {
                echo "<td>" . htmlspecialchars($field) . "</td>";
        }
        echo "</tr>\n";
}

This is a safe way to read csv lines as it is unicode safe and also supports fields that are wrapped in double quotes.

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

5 Comments

even when some fields are empty? does it display neatly?
Yes it will. Please try it.
Another thing, In the future I will using this(datas) to compare on other csv file. If the date and payment amount are match to the other csv file it will be mark as MATCHED. So the easy access of each data by row here is obsolete.
Comparing csv lines like that is dangerous. What if the two csv lines had exact same data but the formatting was different? Say one had fields wrapped in double quotes while the other didn't?
Thanks for your answer. Is there a way from your code that the displaying of data would be like this? <td><?= $name?></td> <td><?= $email?></td> <td><?= $address?></td> <td><?= $payment?></td> <td><?= $datepaid?></td>
1

You should use fgetcsv:

$fp = @fopen('data/payment.csv', 'r');
$tbl = '<table><thead><tr><th>NAME</th><th>Email</th><th>Address</th><th>Payment</th><th>Datepaid</th></tr></thead><tbody>';
while($r = fgetcsv($fp)){
  $tbl .= '<tr>';
  foreach($r as $v){
    $tbl .= '<td>'.htmlentities($v, ENT_QUOTES, 'UTF-8').'</td>';
  }
  $tbl .= '</tr>';
}
$tbl .= '</tbody></table>';
echo $tbl; // echo $tbl wherever you want

1 Comment

Thanks for help @PHPglue.

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.