HTML to CSV PHP

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.

 

 

TLDR – QUICK SLIDES

Download & Notes

 

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,[email protected],465785
Joa Doe,[email protected],123456
Job Doe,[email protected],234567
Joe Doe,[email protected],345678
Jog Doe,[email protected],578456
Joh Doe,[email protected],378945
Joi Doe,[email protected],456789
Jon Doe,[email protected],987654
Jor Doe,[email protected],754642
Joy Doe,[email protected],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.
  • If the cell contains a comma, the value will be enclosed in quotes. E.G. "Jon, Doe"

 

 

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 = fgetcsv($stream)) !== false) {
    echo "<tr>";
    foreach ($row as $col) { echo "<td>$col</td>"; }
    echo "</tr>";
  }
 
  // (C) CLOSE CSV FILE
  fclose($stream);
?></table>

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

 

THE FIRST ROW IS THE HEADER

$first = true;
while (($row = fgetcsv($stream)) !== false) {
  echo "<tr>";
  if ($first) {
    foreach ($row as $col) { echo "<th>$col</th>"; }
    $first = false;
  } else { foreach ($row as $col) { echo "<td>$col</td>"; } }
  echo "</tr>";
}

Just add a simple

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

 

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.

  • Display CSV As Table In Javascript – Code Boxx
  • Simple Sortable Table With HTML Javascript – Code Boxx
  • Ways To Read Files In PHP – Code Boxx

 

INFOGRAPHIC CHEAT SHEET

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!

How do I convert HTML to csv?

How to convert HTML to CSV.
Upload html-file(s) Select files from Computer, Google Drive, Dropbox, URL or by dragging it on the page..
Choose "to csv" Choose csv or any other format you need as a result (more than 200 formats supported).
Download your csv..

How to download HTML table as csv in PHP?

Export Data to CSV File using PHP (exportData..
Retrieve data from the MySQL database..
Create a file pointer using fopen() function..
Specify the header columns and put data into the CSV file..
Output each row of the data, format line as CSV, and write to file pointer..

How to export data to CSV file in PHP?

How to export data to CSV?.
Step 1: Create Header. ... .
Step 2: Force the output buffer to output CSV with a specific filename. ... .
Step 3: Create a file using PHP. ... .
Step 4: Clean Up Output Buffer. ... .
Step 5: Write Header to CSV File. ... .
Step 6: Write actual content to CSV File..

How to export HTML table to Excel in PHP?

So let's start implementing HTML Table data export to to CSV, Excel and Text file with jQuery, PHP and MySQL..
Step1: Create MySQL Database Table. ... .
Step2: Include jQuery, Bootstrap and tableExport Files. ... .
Step3: Display Records in HTML Table. ... .
Step4: Export HTML Table Data..