Display CSV As HTML Table In PHP (Simple Example)

Welcome to a tutorial on how to read a CSV file and display it as an HTML table in PHP. So you want to read a CSV file somewhere on the server, and generate an HTML table with it? Well, it is actually stupidly simple to do, no third-party libraries required – Read on for the example!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes CSV Table Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

CSV TO HTML TABLE

All right, let us now get into the example of generating an HTML table from a CSV file.

 

PART 1) DUMMY CSV FILE

1-dummy.csv
Jo Doe,jo@doe.com,465785
Joa Doe,joa@doe.com,123456
Job Doe,job@doe.com,234567
Joe Doe,joe@doe.com,345678
Jog Doe,jog@doe.com,578456
Joh Doe,joh@doe.com,378945
Joi Doe,joi@doe.com,456789
Jon Doe,jon@doe.com,987654
Jor Doe,jor@doe.com,754642
Joy Doe,joy@doe.com,124578

Let us start with the dummy CSV file. For the folks who have no idea of how a CSV (comma-separated values) file works:

  • CSV files are just plain text.
  • We use line breaks \r\n to indicate rows.
  • We use commas , to indicate columns.

 

 

PART 2) CSV TO HTML TABLE

2-csv-table.php
<table id="demo"><?php
  // (A) OPEN CSV FILE
  $stream = fopen("1-dummy.csv", "r");
 
  // (B) EXTRACT ROWS & COLS
  while (($row = fgets($stream)) !== false) {
    echo "<tr>";
    $col = explode(",", $row);
    foreach ($col as $c) { echo "<td>$c</td>"; }
    echo "</tr>";
  }
 
  // (C) CLOSE CSV FILE
  fclose($stream);
?></table>

  • $stream = fopen("1-dummy.csv", "r") opens the CSV file.
  • while (($row = fgets($stream)) !== false) Reads the CSV file row-by-row.
  • echo "<tr>" Self-explanatory…
  • $col = explode(",", $row) Remember that columns are separated using commas? This splits the current row into columns.
  • foreach ($col as $c) { echo "<td>$c</td>" Self-explanatory once again.

Yep, that’s all it takes to “convert CSV to HTML table”. For real.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

For you guys who are thinking “upload CSV file to the server, then use PHP to display it as a table” – There’s no need to do so. Although kind of limited at the time of writing, modern Javascript can directly read the CSV file and generate the HTML table. Check out “CSV As Table in Javascript” below.

 

INFOGRAPHIC CHEAT SHEET

CSV To HTML Table In PHP (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published.