Working with Data on the Web
Illustration by John Hersey

Parsing CSV files


From:

Working with Data on the Web

with Joseph Lowery

Video: Parsing CSV files

Let's say you've got a CSV file, and you want to display data from it on your web page. How would you go about that? In this lesson, I'll show you how to do it exactly in PHP. In the previous lesson, you saw how we took data entered by a website visitor into this form, and wrote it out to a CSV file. After the data was entered, we then displayed a, thank you page. So let's take that, thank you page a step further to illustrate how we can read an existing CSV file.

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
please wait ...
Watch the Online Video Course Working with Data on the Web
2h 21m Intermediate Aug 21, 2013

Viewers: in countries Watching now:

Often, designers need to convey simple data without the overhead of a relational database such as MySQL. This course introduces web technologies that make it easy for a designer or developer to store, retrieve, and display data online.

First, 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.

Topics include:
  • 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
Subjects:
Developer Web
Software:
HTML XML CSS JSON YAML
Author:
Joseph Lowery

Parsing CSV files

Let's say you've got a CSV file, and you want to display data from it on your web page. How would you go about that? In this lesson, I'll show you how to do it exactly in PHP. In the previous lesson, you saw how we took data entered by a website visitor into this form, and wrote it out to a CSV file. After the data was entered, we then displayed a, thank you page. So let's take that, thank you page a step further to illustrate how we can read an existing CSV file.

We'll add in PHP code to open up the file, get the last entered record, and then display those individual data pieces in a table format. We're going to to switch over to my code editor where I have the, thanks.php file from chapter 2_02 folder open. First, I'll need to add my necessary PHP code right at the top of the page. So lets put in our PHP code block.

And then within that we'll enter in three lines of code. The first line will read all of the text file into an array. Because it's organized by rows, I'll make that my variable name, and we'll use the PHP function file. It takes one argument and that's the name of the file you're reading. Mine is data.csv. Then, we'll use the array pop function, to just get the last element in the array. That's the most recently entered record, and store that in the variable called last row.

So, last_row equals array_pop parenthesis and rows variable. Finally, we'll use a function called strgetcsv. To parse that last CSV string into an array. Which I've called rec, short for record. One note, the function str_getcsv, or string get csv, is a relatively new one, so you'll want to make sure that your web host is running PHP 5.3, or higher.

So, rec is my variable name, and we'll set that equal to. Str_getcsv($last_row) okay, that should take care of getting our last CSV record. So how do we display the data? Pretty easy. We just echo the desired array element. Now, I've already created a basic table so let's scroll down And there you see, it's starting online 42, and we'll add in our PHP code.

And it's basically going to go in the TD tag, the table data cell, where you see non breaking spaces. So, I'll go to the first one which is the name. Highlight that non breaking space. And replace it with a bit of PHP code or write echo, the variable rec, and then square bracket for an array. And the first element that array which is the 0 element. And then, close off my PHP value. Okay. That's basically the set up.

Let's copy that, because we're going to be using this basic code over and over again, and just changing the array number. Alright, with that copied, I'll go to non-breaking space for email, and change the array from 0 to 1. And then let's go to cell phone, paste it in again, change the array number to 2. Not a whole lot of suspense here, is there, folks? And for date of birth, that will be 3. And finally, for years of art study, it's 4. Well, that should do it.

Let's save the file and check it out. I've got my index.php file from the same exercise file folder open in my browser. No need to refresh it because we are just working on the thanks.php file. So I'm going to fill out the form with new data, I'll scroll down a bit here. This time, I'm kind of in the pet mood so I think I'll give my Cat top billing, and that is of course Chuck the cat which we like to do from time to time. A little email address for Chuck. Her own phone number.

And a nice birthday. Chuck's been with us for a while. OK. Let's click Submit and there you have it. There's a confirmation of all of our entered data nicely presented. Of course, we could have just as easily pulled the data directly from the submitted form, but that's not really the point. Now, you have a technique for getting your data directly from the CSV file, to display online. Pretty cool, right?

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

 
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

Congratulations

You have completed Working with Data on the Web.

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


OK
Become a member to add this course to a playlist

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

Get started

Already a member ?

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:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
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.

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 lynda.com 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 lynda.com 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 lynda.com.

Sign up and receive emails about lynda.com 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.