Parsing CSV files
Video: Parsing CSV filesLet'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.
- Next steps
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.
- 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
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.