Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

How to Store Data as a Comma-Separated Values

Storing data as comma-separated values provides you with in-depth training on Developer. Taught by J… Show More

Working with Data on the Web

with Joseph Lowery

Video: How to Store Data as a Comma-Separated Values

Storing data as comma-separated values provides you with in-depth training on Developer. Taught by Joseph Lowery as part of the Working with Data on the Web
please wait ...
Storing data as comma-separated values
Video duration: 10m 17s 2h 21m Intermediate


Storing data as comma-separated values provides you with in-depth training on Developer. Taught by Joseph Lowery as part of the Working with Data on the Web

Developer Web

Storing data as comma-separated values

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.

There are currently no FAQs about Working with Data on the Web.






Don't show this message again
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.

Mark all as unwatched Cancel


You have completed Working with Data on the Web.

Return to your organization's learning portal to continue training, or close this page.


Upgrade to View Courses Offline


With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

Become a Member and Create Custom Playlists

Join today and get unlimited access to the entire library of online learning video courses—and create as many playlists as you like.

Get started

Already a member?

Log in

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

You started this assessment previously and didn’t complete it.

You can pick up where you left off, or start over.

Resume Start over

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from

Sign up and receive emails about and our online training library:

Here’s our privacy policy with more details about how we handle your information.

submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.