1
00:00:00,440 --> 00:00:03,690
The ease of working with JSON
is one of the main reasons

2
00:00:03,690 --> 00:00:07,900
it has become such a popular format for
sharing data on the web.

3
00:00:07,900 --> 00:00:13,120
We can take any object or
array from PHP and turn it into a JSON

4
00:00:13,120 --> 00:00:17,930
object that can then be shared without
needing to rely on a particular language.

5
00:00:18,950 --> 00:00:21,650
I can share the JSON for
my recommendations and

6
00:00:21,650 --> 00:00:26,120
they can be easily read using Python or
any other language.

7
00:00:26,120 --> 00:00:29,570
Communication is what has made
the Internet such a powerful force.

8
00:00:30,810 --> 00:00:33,015
Let's create another file
in the inc directory.

9
00:00:33,015 --> 00:00:38,796
We'll name this one

10
00:00:38,796 --> 00:00:44,923
write_json [SOUND].

11
00:00:44,923 --> 00:00:47,145
We'll start by creating an array for
the new book.

12
00:00:47,145 --> 00:00:56,200
[SOUND]
This time,

13
00:00:56,200 --> 00:00:59,370
we'll be using the FormData
from the Add book form.

14
00:00:59,370 --> 00:01:02,229
And our array will be
an associative array.

15
00:01:04,754 --> 00:01:09,081
Our title will be filter_

16
00:01:09,081 --> 00:01:15,375
input(INPUT_POST, 'title, and

17
00:01:15,375 --> 00:01:20,890
FILTER_SANITIZE_STRING..

18
00:01:21,900 --> 00:01:26,430
We'll copy this for link,
book, image, book description,

19
00:01:26,430 --> 00:01:30,110
pages, author, ISBN,
rating and publish date.

20
00:01:34,180 --> 00:01:39,713
Link, [SOUND] and

21
00:01:39,713 --> 00:01:46,040
this will be a URL.

22
00:01:46,040 --> 00:01:47,616
Our book_image_url,
[SOUND] which will also be a URL.

23
00:01:47,616 --> 00:01:49,655
[SOUND]

24
00:01:49,655 --> 00:01:58,628
Our book_description,

25
00:01:58,628 --> 00:02:03,114
[SOUND]
number

26
00:02:03,114 --> 00:02:08,007
of pages [SOUND]

27
00:02:08,007 --> 00:02:12,492
which will be

28
00:02:12,492 --> 00:02:18,214
a NUMBER_INT.

29
00:02:18,214 --> 00:02:24,926
[SOUND] author_name,

30
00:02:24,926 --> 00:02:29,871
[SOUND] isbn [SOUND]

31
00:02:29,871 --> 00:02:33,756
which will be

32
00:02:33,756 --> 00:02:38,708
a NUMBER_INT.

33
00:02:38,708 --> 00:02:41,706
[SOUND] Our

34
00:02:41,706 --> 00:02:49,204
average_rating [SOUND]

35
00:02:49,204 --> 00:02:53,326
which will be

36
00:02:53,326 --> 00:02:58,580
a number float.

37
00:02:58,580 --> 00:03:05,204
And finally book_published.

38
00:03:05,204 --> 00:03:10,804
[SOUND] And this will

39
00:03:10,804 --> 00:03:17,811
also be a NUMBER_INT.

40
00:03:17,811 --> 00:03:21,122
Once again, we need to get
the contents of the JSON file and

41
00:03:21,122 --> 00:03:25,000
use the JSON decode function
to turn it into an object.

42
00:03:25,000 --> 00:03:27,390
We'll be using this
file a couple of times.

43
00:03:27,390 --> 00:03:28,920
So we'll set this up as a variable.

44
00:03:31,020 --> 00:03:33,634
Our file = '

45
00:03:33,634 --> 00:03:43,634
../data/json/top_programming_books.json.

46
00:03:45,296 --> 00:03:46,746
books

47
00:03:46,746 --> 00:03:56,898
=json_decode(file_get_contents

48
00:03:56,898 --> 00:04:00,380
and the file.

49
00:04:02,170 --> 00:04:04,893
Next, let's make sure
the object is as expected.

50
00:04:09,577 --> 00:04:19,132
Is_object($books->collection->books,

51
00:04:19,132 --> 00:04:23,910
and the first array item.

52
00:04:31,350 --> 00:04:33,843
Now we can add the new book
to our book collection.

53
00:04:45,569 --> 00:04:51,373
Next, we can use the json_encode function
to turn our object back into JSON.

54
00:05:02,080 --> 00:05:04,729
Before we actually write
this back to our file,

55
00:05:04,729 --> 00:05:09,047
let's take a quick look at the JSON
itself, by displaying it to the browser.

56
00:05:11,594 --> 00:05:13,210
Make sure we use the pretags.

57
00:05:21,584 --> 00:05:23,020
And let's preview this in the browser.

58
00:05:24,230 --> 00:05:25,670
We're going to add a book.

59
00:05:27,590 --> 00:05:30,055
I'm going to pull
the information from good reads.

60
00:06:07,091 --> 00:06:10,560
There's no ISBN, so I'll leave that blank.

61
00:06:13,239 --> 00:06:15,350
And the publish date was 2016.

62
00:06:15,350 --> 00:06:18,566
I wanna pull the smaller image.

63
00:06:31,663 --> 00:06:32,440
And let's add the book.

64
00:06:34,400 --> 00:06:37,690
We have the json but
it's not the prettiest.

65
00:06:37,690 --> 00:06:41,560
Let's add a couple options
to our json encode function.

66
00:06:41,560 --> 00:06:45,556
See the notes associated with this
video for more available options.

67
00:06:49,293 --> 00:06:51,867
We'll use

68
00:06:51,867 --> 00:06:57,874
JSON_PRETTY_PRINT |

69
00:06:57,874 --> 00:07:05,320
JSON_UNESCAPED_SLASHES.

70
00:07:05,320 --> 00:07:06,550
Now, let's preview this again.

71
00:07:09,770 --> 00:07:11,020
Much better.

72
00:07:11,020 --> 00:07:15,365
Let's write this back to our file and
finish by redirecting to the books page.

73
00:07:18,032 --> 00:07:22,333
Let's comment out the echo.

74
00:07:22,333 --> 00:07:29,320
file_put_contents, file and our json.

75
00:07:33,275 --> 00:07:38,183
Header is location: /books.php.

76
00:07:38,183 --> 00:07:42,896
Let's check out the final
functionality in the browser.

77
00:07:45,865 --> 00:07:52,050
We fill out a form, submit it,
and now our book shows up.

