1
00:00:00,290 --> 00:00:02,930
So reading CSV files is handy and

2
00:00:02,930 --> 00:00:07,000
we were able to use that data
to display a suggestion page.

3
00:00:07,000 --> 00:00:09,790
But what if I want to
add more suggestions?

4
00:00:09,790 --> 00:00:12,850
I'm always discovering new things and
meeting new people.

5
00:00:12,850 --> 00:00:16,300
So, I'm sure that I will have
more suggestions in the future.

6
00:00:16,300 --> 00:00:18,940
We could open the CSV file directly, but

7
00:00:18,940 --> 00:00:23,200
that wouldn't work if I wanted to
fill out a form that adds a person.

8
00:00:23,200 --> 00:00:27,490
So let's look at using PHP to add
an additional line to our CSV file.

9
00:00:28,740 --> 00:00:31,236
Let's create a new file
in the inc directory.

10
00:00:33,734 --> 00:00:38,432
We'll name this write_csv.php.

11
00:00:41,679 --> 00:00:45,308
We'll start by creating an array for
the new contact.

12
00:00:53,208 --> 00:00:57,110
We'll be using the form data
from our add person form.

13
00:00:57,110 --> 00:00:58,429
So, we want to filter our input.

14
00:01:07,337 --> 00:01:08,313
First.

15
00:01:09,857 --> 00:01:19,284
FILTER_SANTIZE_STRING, Last website,
Twitter and image.

16
00:01:29,060 --> 00:01:32,640
The website and
the image will actually be URLs.

17
00:01:32,640 --> 00:01:36,017
So, we can change this
to FILTER_SANITIZE_URL.

18
00:01:44,038 --> 00:01:47,520
Now use fopen to open our CSV file.

19
00:01:49,380 --> 00:01:52,091
Once again,
wrapping it in an if statement.

20
00:01:59,288 --> 00:02:07,245
We'll need to go up one level before
we enter data/csv/people.csv.

21
00:02:09,641 --> 00:02:13,569
This time we'll use a for
the mode meaning append.

22
00:02:13,569 --> 00:02:17,826
It opens the file and places the file
pointer at the end of the file.

23
00:02:26,694 --> 00:02:28,192
And fclose.

24
00:02:32,967 --> 00:02:36,664
We can now use fputcsv.

25
00:02:36,664 --> 00:02:40,942
Passing the file handler just
like we did for the fgetcsv.

26
00:02:40,942 --> 00:02:44,048
This time we also passed the new array.

27
00:02:46,783 --> 00:02:50,679
And we'll finish off this file by
redirecting to the people page.

28
00:02:59,458 --> 00:03:01,030
Let's give this a try in the browser.

29
00:03:02,530 --> 00:03:07,310
Go to Add Person, and fill out the form.

30
00:03:07,310 --> 00:03:09,988
I'm going to add the Treehouse CEO,
Ryan Carson.

31
00:03:09,988 --> 00:03:11,480
He's a great person to follow.

32
00:03:18,920 --> 00:03:21,267
Let's grab his profile
picture from Twitter.

33
00:03:24,157 --> 00:03:27,681
If you right click,
you can copy image address.

34
00:03:36,247 --> 00:03:40,010
We were redirected to the People page,
but it didn't show up right.

35
00:03:40,010 --> 00:03:41,920
So, let's take a look at the CSV file.

36
00:03:43,030 --> 00:03:47,171
We can't read CSV files in Workspaces,
so we need to rename it first.

37
00:03:55,030 --> 00:03:57,129
We'll change this to txt.

38
00:04:01,383 --> 00:04:05,750
The problem is that there is no new
line between the last person and Ryan.

39
00:04:06,880 --> 00:04:11,840
If the file doesn't end on a new line when
we try to append, this could be a problem.

40
00:04:11,840 --> 00:04:14,385
Let's delete this edition and
rename the file again.

41
00:04:16,747 --> 00:04:19,010
I'm going to show you how to check for
a new line.

42
00:04:19,010 --> 00:04:21,639
So, let's make sure this
does not end on a new line.

43
00:04:30,170 --> 00:04:34,192
First, we need to be able to read,
as well as write.

44
00:04:34,192 --> 00:04:36,550
So, let's add a plus to the mode.

45
00:04:37,565 --> 00:04:43,687
Before we call fputcsv,
we're going to use the fseek function.

46
00:04:47,149 --> 00:04:50,380
This will allow us to
move the file pointer.

47
00:04:50,380 --> 00:04:54,940
The fseek function accepts three
parameters, the file handler,

48
00:04:54,940 --> 00:05:00,235
the offset, which in this case
we want to move back one, so

49
00:05:00,235 --> 00:05:06,728
-1, and wents, which tells the point
where you are starting from.

50
00:05:06,728 --> 00:05:10,070
We'll use SEEK_END.

51
00:05:11,230 --> 00:05:15,100
Because we want to go back one
byte from the end of the file.

52
00:05:15,100 --> 00:05:18,110
Now, we can check if our
file ends in a new line.

53
00:05:18,110 --> 00:05:20,900
And if not,
add a new end of line character.

54
00:05:24,295 --> 00:05:25,733
If fgets.

55
00:05:28,908 --> 00:05:32,505
Is not equal to the end of line.

56
00:05:38,669 --> 00:05:40,425
Then we're going to fputs.

57
00:05:44,110 --> 00:05:45,638
A new end of line.

58
00:05:47,409 --> 00:05:50,529
Let's go back to the browser and
resubmit the form.

59
00:05:58,229 --> 00:06:01,197
This time,
our people page shows the new suggestion

60
00:06:01,197 --> 00:06:03,900
properly because it was
placed on its own line.

