Home

How to Store and View CSV Data in PHP for HTML5 and CSS3 Programming

|
Updated:  
2016-03-26 13:14:09
|
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon

This basic mechanism for storing data in PHP is great for small amounts of data, but it will quickly become unwieldy for HTML5 and CSS3 programming if you're working with a lot of information. If you're expecting hundreds or thousands of people to read your forms, you'll need a more organized way to store the data.

You can store data in a very basic text format that can be easily read by spreadsheets and databases. This has the advantage of imposing some structure on the data and is still very easy to manage.

The basic plan is to format the data in a way that it can be read back into variables. Generally, you store all of the data for one form on a single line, and you separate the values on that line with a delimiter, which is simply some character intended to separate data points.

Spreadsheets have used this format for many years as a basic way to transport data. In the spreadsheet world, this type of file is called a CSV (for comma-separated values) file. However, the delimiter doesn't need to be a comma. It can be nearly any character.

How to store data in a CSV file

Here's how you store data in a CSV file:

  1. You can use the same HTML form.

    The data is gathered in the same way regardless of the storage mechanism. There is a new page called addContactCSV.html, but the only difference between this file and the addContact.html page is the property. You can have the two pages send the data to different PHP programs, but everything else is the same.

  2. Read the data as normal.

    In your PHP program, you begin by pulling data from the previous form.

     $lName = filter_input(INPUT_POST, "lName");
     $fName = filter_input(INPUT_POST, "fName");
     $email = filter_input(INPUT_POST, "email");
     $phone = filter_input(INPUT_POST, "phone");
  3. Store all the data in a single tab-separated line.

    Concatenate a large string containing all the data from the form. Place a delimiter (like the tab symbol t) between variables and a newline (n) at the end.

    //generate output for text file
    $output = $fName . "t";
    $output .= $lName . "t";
    $output .= $email . "t";
    $output .= $phone . "n";
  4. Open a file in append mode.

    This time, the file is named contacts.csv to help remind the user that the contact form is now stored in a CSV format.

  5. Write the data to the file.

    The fwrite() function does this job with ease.

  6. Close the file.

    This part (like most of the program) is identical to the earlier version of the code.

Here's the code for addContactCSV.php in its entirety:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>addContactCSV.php</title>
 <link rel = "stylesheet"
  type = "text/css"
  href = "contact.css" />
</head>
<body>
 <?php
 //read data from form
 $lName = filter_input(INPUT_POST, "lName");
 $fName = filter_input(INPUT_POST, "fName");
 $email = filter_input(INPUT_POST, "email");
 $phone = filter_input(INPUT_POST, "phone");
 //print form results to user
 print <<< HERE
 <h1>Thanks!</h1>
 <p>
  Your spam will be arriving shortly.
 </p>
 <p>
 first name: $fName <br />
 last name: $lName <br />
 email: $email <br />
 phone: $phone
 </p>
HERE;
 //generate output for text file
 $output = $fName . "t";
 $output .= $lName . "t";
 $output .= $email . "t";
 $output .= $phone . "n";
 //open file for output
 $fp = fopen("contacts.csv", "a");
 //write to the file
 fwrite($fp, $output);
 fclose($fp);
 ?>
</body>
</html>

As you can see, this is not a terribly difficult way to store data.

How to view CSV data directly

This is what the resulting file looks like in a plain text editor.

image0.jpg

Of course, CSV data isn't meant to be read as plain text. On most operating systems, the .csv file extension is automatically linked to the default spreadsheet program. If you double-click the file, it will likely open in your spreadsheet.

image1.jpg

This is an easy way to store large amounts of data because you can use the spreadsheet to manipulate the data. Of course, relational databases are even better, but this is a very easy approach for relatively simple data sets.

About This Article

This article is from the book: 

About the book author:

Andy Harris earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].