1 00:00:00,470 --> 00:00:05,420 CSV is very popular format for importing and exporting data to and 2 00:00:05,420 --> 00:00:07,480 from databases and spreadsheets. 3 00:00:08,740 --> 00:00:13,580 These files usually have a header row and then multiple rows below them 4 00:00:13,580 --> 00:00:17,950 with each column of values separated by the previous column by a comma. 5 00:00:19,040 --> 00:00:22,880 We'll be using some built in PHP functions that let us read and 6 00:00:22,880 --> 00:00:25,920 write CSV data as an array. 7 00:00:25,920 --> 00:00:29,560 I have included a contact list in a CSV file 8 00:00:29,560 --> 00:00:33,630 that will be using to generate the page of suggested people to follow. 9 00:00:33,630 --> 00:00:34,380 Let's get started. 10 00:00:35,430 --> 00:00:38,620 Let's open the people.php file where I have started 11 00:00:38,620 --> 00:00:44,160 by showing the contents of the CSV file and also a sample contact. 12 00:00:44,160 --> 00:00:50,251 Let's preview this in a browser, As you can 13 00:00:50,251 --> 00:00:56,050 see this is a comma separated file with a header row and here's the sample contact. 14 00:00:56,050 --> 00:00:59,420 Let's go back to work spaces and remove the file display, and 15 00:00:59,420 --> 00:01:02,520 replace the sample contact with the data from the CSV. 16 00:01:02,520 --> 00:01:06,480 We'll start by commenting out these lines. 17 00:01:11,408 --> 00:01:16,480 Now, we need to open a file for reading like we did in the first section. 18 00:01:16,480 --> 00:01:18,420 We're going to wrap this in an if statement. 19 00:01:24,716 --> 00:01:30,887 Fopen 'data/csv/people.csv' and 20 00:01:30,887 --> 00:01:34,520 we will open it to read. 21 00:01:44,703 --> 00:01:45,203 And fclose. 22 00:01:48,231 --> 00:01:53,373 Instead of fgets, to get each line as a string, we'll use fget csv. 23 00:01:56,838 --> 00:02:01,101 This will read each line as a row of columns in a CSV file. 24 00:02:01,101 --> 00:02:04,680 This will return an array of columns. 25 00:02:08,410 --> 00:02:12,579 There are a number of optional parameters including the ability to 26 00:02:12,579 --> 00:02:14,750 specify the delimiter. 27 00:02:14,750 --> 00:02:20,800 By default, it uses a comma but you can read tab delimited files as well. 28 00:02:20,800 --> 00:02:24,060 We won’t be using any of the optional parameters right now but 29 00:02:24,060 --> 00:02:27,070 make sure you check the notes associated with this video. 30 00:02:27,070 --> 00:02:31,710 The first time that we use the fgetcsv function, it should return our headers and 31 00:02:31,710 --> 00:02:33,210 move the pointer to the next line. 32 00:02:36,825 --> 00:02:38,060 Let's check out the headers. 33 00:02:43,892 --> 00:02:45,050 Let's preview this in a browser. 34 00:02:47,090 --> 00:02:49,930 I'm going to use those headers to make my code more readable. 35 00:02:51,520 --> 00:02:55,390 First, I'm going to flip the array so that the key is a value and 36 00:02:55,390 --> 00:02:56,410 the value is the key. 37 00:03:02,003 --> 00:03:07,030 I then use extract to put each of those keys into its own variable. 38 00:03:12,419 --> 00:03:18,810 Now I can echo first and this should give me the number associated with that column. 39 00:03:21,950 --> 00:03:25,535 Great, now we're going to loop through each of the additional lines to 40 00:03:25,535 --> 00:03:26,740 display each contact. 41 00:03:29,020 --> 00:03:32,190 Let's comment out this line and this line. 42 00:03:36,540 --> 00:03:42,694 While contact = fgetcsv and 43 00:03:42,694 --> 00:03:46,800 the file handler. 44 00:03:48,485 --> 00:03:50,170 While it's not equal to false. 45 00:03:53,576 --> 00:03:57,920 This will loop through the file until there are no more lines to read. 46 00:03:57,920 --> 00:03:59,850 I'm going to wrap this around my sample contact. 47 00:04:11,292 --> 00:04:15,410 I want the loop to be on the inside of the row div. 48 00:04:15,410 --> 00:04:17,380 Then I'll always have at least one row. 49 00:04:28,660 --> 00:04:32,551 Then I want to create a new row for every four contacts, so 50 00:04:32,551 --> 00:04:35,400 I'll need to count which number I'm on. 51 00:04:42,792 --> 00:04:44,496 I already have my first row, 52 00:04:44,496 --> 00:04:48,260 so I don't want to add another row if this is the first contact. 53 00:04:49,820 --> 00:04:56,150 If count is greater than zero, and 54 00:04:56,150 --> 00:04:59,230 count, and the modulus operator. 55 00:04:59,230 --> 00:05:01,260 This will give me the remainder. 56 00:05:01,260 --> 00:05:03,360 And I want to divide this by 4. 57 00:05:03,360 --> 00:05:06,670 If that has no remainder, 58 00:05:06,670 --> 00:05:11,410 then I know that it's divisible by 4. 59 00:05:13,064 --> 00:05:15,993 Then I can close my div. 60 00:05:19,879 --> 00:05:20,780 And start a new row. 61 00:05:32,574 --> 00:05:33,770 And then increase my count. 62 00:05:35,140 --> 00:05:39,240 Now we can use the contact array with the header variables we created 63 00:05:39,240 --> 00:05:41,210 to fill in the details for each contact. 64 00:05:52,294 --> 00:05:57,884 Make sure we put 65 00:05:57,884 --> 00:06:03,474 a space between 66 00:06:03,474 --> 00:06:09,064 the first, and 67 00:06:09,064 --> 00:06:13,370 last name. 68 00:07:13,330 --> 00:07:16,150 Okay, let's take a look at our page in the browser now. 69 00:07:19,220 --> 00:07:21,200 My images aren't showing up right let's go check that. 70 00:07:23,210 --> 00:07:26,950 The image actually has the full length to the source, so let's remove this. 71 00:07:32,132 --> 00:07:35,053 Great, each of the contacts from the CSV file is 72 00:07:35,053 --> 00:07:38,790 displayed with links to their website and Twitter account.