CSV is very popular format for importing and exporting data to and
from databases and spreadsheets.
These files usually have a header row and then multiple rows below them
with each column of values separated by the previous column by a comma.
We'll be using some built in PHP functions that let us read and
write CSV data as an array.
I have included a contact list in a CSV file
that will be using to generate the page of suggested people to follow.
Let's get started.
Let's open the people.php file where I have started
by showing the contents of the CSV file and also a sample contact.
Let's preview this in a browser, As you can
see this is a comma separated file with a header row and here's the sample contact.
Let's go back to work spaces and remove the file display, and
replace the sample contact with the data from the CSV.
We'll start by commenting out these lines.
Now, we need to open a file for reading like we did in the first section.
We're going to wrap this in an if statement.
Fopen 'data/csv/people.csv' and
we will open it to read.
And fclose.
Instead of fgets, to get each line as a string, we'll use fget csv.
This will read each line as a row of columns in a CSV file.
This will return an array of columns.
There are a number of optional parameters including the ability to
specify the delimiter.
By default, it uses a comma but you can read tab delimited files as well.
We won�t be using any of the optional parameters right now but
make sure you check the notes associated with this video.
The first time that we use the fgetcsv function, it should return our headers and
move the pointer to the next line.
Let's check out the headers.
Let's preview this in a browser.
I'm going to use those headers to make my code more readable.
First, I'm going to flip the array so that the key is a value and
the value is the key.
I then use extract to put each of those keys into its own variable.
Now I can echo first and this should give me the number associated with that column.
Great, now we're going to loop through each of the additional lines to
display each contact.
Let's comment out this line and this line.
While contact = fgetcsv and
the file handler.
While it's not equal to false.
This will loop through the file until there are no more lines to read.
I'm going to wrap this around my sample contact.
I want the loop to be on the inside of the row div.
Then I'll always have at least one row.
Then I want to create a new row for every four contacts, so
I'll need to count which number I'm on.
I already have my first row,
so I don't want to add another row if this is the first contact.
If count is greater than zero, and
count, and the modulus operator.
This will give me the remainder.
And I want to divide this by 4.
If that has no remainder,
then I know that it's divisible by 4.
Then I can close my div.
And start a new row.
And then increase my count.
Now we can use the contact array with the header variables we created
to fill in the details for each contact.
Make sure we put
a space between
the first, and
last name.
Okay, let's take a look at our page in the browser now.
My images aren't showing up right let's go check that.
The image actually has the full length to the source, so let's remove this.
Great, each of the contacts from the CSV file is
displayed with links to their website and Twitter account.