Join Joseph Lowery for an in-depth discussion in this video Storing data as comma-separated values, part of Working with Data on the Web.
If you're working with CSV files, where because you want to make it easy for your client to extract the data, or because you just want something basic you'll need some way to store your data as comma separated values. In this lesson, I'll show you a technique that uses PHP to do just that. Of course, whichever server-side technology you use, will have similar capabilities. So, I'll carefully explain the concepts. I have the index.php open in my code editor, that's from chapter 2 0 2 0 1 folder of the exercise files.
It's basically a simple form, let me scroll down a bit so you can see the code. It's in the div container there and as you can see, it's a series of simple input types, that was demo in the previous chapter. But we don't have anything to do with HTML. l We're going to put in our PHP code block up top, before the DOC type HTML. So I start off. By just creating my PHP code block, and now I'll add an if statement that checks to see if the Submit button was posted. In other words, if it was clicked so if is set and then another parenthesis. $_Post.
Square brackets, single quotes, submit. That's the name, or ID, of the field, and then after the end of that line, with all of its closing square brackets and parentheses, put a curly brace and there's our if statement. Now that submit has done its job, checking to see if was click, we really don't need it anymore for this function and we certainly don't want it showing up in the data over and over again. So, let's remove it from the array for both the keys, which makes up our header.
And the values. To do that, we'll establish first a variable, and I'm going to just call this post and set that equal to, $,_POST and now we'll use the Unset function to pop the Submit off the array. Unset, parenthesis, then our variable post, followed by square brackets, single quotes, submit.
And let's put a closing semicolon at then end. Now we're ready to get our data and head values. First, we'll get the values. Again, we'll use a variable, I'm going to call this one data. And we'll set that to another function array value. And array values we're going to get are from our post object. Now let's get to the keys, or the headers because it will be the header row. So I'm going to call this headers.
And again, another PHP function instead of array value, it's array keys. Also from post. Next up, we'll try to open our CSV file and if successful, set up a routine. So this is another if statement, if parenthesis $ fp equals to fopen parenthesis the file we're going to open in single quotes is data.csv after the closing single quote put in a comma and then end the quotes.
A plus, close that single quote, and then at the end of the line, after the parenthesis, a pair of curly phrases. The A plus argument, opens the file for reading or writing, and if it doesn't exist, creates it. So now let's get our first line of the file by declaring a variable line, and set that equal to fgets. That's file gets, and the argument will pass is the file variable.
which is our open file: FP. Next we want to set up an if statement that checks to see whether the first line that we got is the same as our headers. And if it's not, we'll put in our code. So if open parentheses not, that's an exclamation mark and then our variable line is equivalent to double equal signs, headers.
If that condition is true, do this. And we'll put in our curly brace pair. So if there are no headers, let's add them. We're going to use the f put csv function that's built right in the PHP five. This takes two arguments, first our file which is dollar sign fp, and then, what do we want to put in there? We want to put in our headers. And then after the header, we want to put in our data.
So we use the same function again. So now our first case is handled, if there are no headers to our file. Put in the headers and then put in the data, which is fine, if there isn't a header row, but what if there is? Let's put in an else statement that just appends the data. Again, we'll use the fput csv function.
Targeting our data file, $fp and the data. Don't for get the closing semicolon. Okay. We're in the home stretch now. Next, all we need to do, is to close the file and redirect when it's done. I'm going to move this curly brace over a bit so that it flows with the indentation of the if statement and then, after that, close our file and we'll use the fclose function, pass it the name of our file in variable form, and then let's do our redirect and we'll use header.
In parenthesis single quotes Location:thanks.php and then I'll finish up with a semicolon. Alright let's try it out. I'll save the file in my code editor and then head over to the browser. Now I've got the file open already, so let me just hit Refresh to get the latest version. And now I'm going to enter some values and hit Submit. So let's put in real name, fake email address Fake cell number. going to have all your fake data ready when you're doing a lot of testing.
And pseudo-date of birth. I've been studying art for many, many years Click Submit, well we've hit an error so that means that something's wrong with the code. Let me head back to the browser for a quick second so we can refresh it when we're ready. And now let's go back to our code editor, and take a look at the code. In a situation like this, if your code editor supports it, color coding can really help you find the problem right away.
So you'll notice that all the PHP functions are in blue, and if I go up the line looking for something that's not blue, I come to line five, where array value is black like regular text instead of blue like a function. So it should be array values. So I'll change that to values, save my code, head back. Let's click Refresh. Let's try that one more time. This time we'll take advantage of having already entered it, and I'll just tab through.
Doesn't seem to be working for the Cell so let's put that pseudo number in again. And this time, maybe I won't be quite so old. That looks good. And I have less years of art study to commit to. All right, now when we click Submit, we get our thank you. So, let's open up our previously blank CSV file and see if our data's there. Let's head back to the code editor. And I'll open up the data file. Which is data.csv. Well, looking good. There's the header row, name, email, cell, dob, short for date of birth, and study. And there's our first set of data.
Now we need to run one more test to make sure everything is copacetic. So back to the browser we go. I'll just hit the Back button and then click Refresh so we can start over. Now enter in another set of data. That my dog by the way. So we'll give Star her own email address, on my account, of course. Such a pooch or is it mooch. Slightly different phone number, and I don't think she's ever studied art. Okay well again we have our thank you, now let's go check the data. Our data file is changed, lets update it.
There's our second row, without an additional header. Now you have a complete PHP routine, for taking the data from any form and storing it in a CSV file. In the next lesson we'll reverse the process and I'll show you how to display data from a CSV file on the web.
Released
8/21/2013First, discover how data is collected via standard and advanced HTML5 forms. Then look at a client-friendly approach to storing that data using simple CSV files. Next, Joseph shows how to store, manage, and style data with the three big players: the granddaddy of static data, XML; the popular JavaScript-based JSON; and the recursively named YAML (YAML Ain't Markup Language), frequently used in Ruby-based projects. Plus, take a close look at basic HTML5 data options, including local storage and the flexible data attribute.
- Interacting with data through HTML forms
- Storing data as comma-separated values
- Saving, retrieving, and displaying data as XML
- Setting up and updating JSON data
- Creating a YAML data file
- Using HTML5 data storage solutions
Share this video
Embed this video
Video: Storing data as comma-separated values