1 00:00:00,510 --> 00:00:04,920 We have the code required to retrieve the item information from the database. 2 00:00:04,920 --> 00:00:08,880 It's time to use those results to display our media items. 3 00:00:08,880 --> 00:00:13,380 Unfortunately, changing the site to use a database instead of a simple array 4 00:00:13,380 --> 00:00:16,320 is going to require us to rewrite everything from scratch. 5 00:00:17,480 --> 00:00:19,608 Gotcha, actually it's not, 6 00:00:19,608 --> 00:00:25,260 the original data.php file contains an array of catalog information. 7 00:00:25,260 --> 00:00:28,060 All we need to do is update that file 8 00:00:28,060 --> 00:00:31,270 to pull the catalog information from the database instead. 9 00:00:32,650 --> 00:00:35,880 After that we can start updating our functions to make 10 00:00:35,880 --> 00:00:38,200 better use of the data from the database. 11 00:00:38,200 --> 00:00:40,280 In the functions file we have the code for 12 00:00:40,280 --> 00:00:44,590 manipulating the catalog information and adding it to our site. 13 00:00:44,590 --> 00:00:48,210 The data.php file contains the data itself. 14 00:00:48,210 --> 00:00:53,260 This is an example of the programming concept known as separating concerns. 15 00:00:53,260 --> 00:00:55,650 As you do more programming, you'll hear more and 16 00:00:55,650 --> 00:00:58,960 more about the concept of separating concerns. 17 00:00:58,960 --> 00:01:02,790 One of the main benefits of separating concerns is that you can make 18 00:01:02,790 --> 00:01:08,280 major changes in one area, without having any effect on the other areas. 19 00:01:08,280 --> 00:01:11,420 For our listing pages, we separate our concerns so 20 00:01:11,420 --> 00:01:13,500 that all we have to change is our data file. 21 00:01:14,790 --> 00:01:19,334 >> Back in the workspace associated with this video, let's change data.php. 22 00:01:20,760 --> 00:01:24,756 By changing this file we won't have to rewrite any of the path to our includes. 23 00:01:24,756 --> 00:01:29,472 Will include the connection.php file and 24 00:01:29,472 --> 00:01:34,585 then we'll comment out the rest of this code. 25 00:01:38,554 --> 00:01:40,890 This will leave it here for reference. 26 00:01:40,890 --> 00:01:44,680 Now let's go back to connection.php. 27 00:01:44,680 --> 00:01:48,480 Our code creates a pdo object that connects to the database. 28 00:01:48,480 --> 00:01:50,820 It runs a query against that database and 29 00:01:50,820 --> 00:01:53,480 displays the result set as an array to the screen. 30 00:01:54,540 --> 00:01:58,200 Let's change this last line, so that instead of displaying the results 31 00:01:58,200 --> 00:02:01,610 to the screen it loads it into a variable called catalog. 32 00:02:05,020 --> 00:02:07,550 The original array of catalog information, 33 00:02:07,550 --> 00:02:10,490 was also stored in a variable name catalog. 34 00:02:10,490 --> 00:02:14,330 So by storing the results of our database query in the same variable 35 00:02:14,330 --> 00:02:16,870 our original code will still work. 36 00:02:16,870 --> 00:02:19,300 Now let's go take a look at the home page. 37 00:02:19,300 --> 00:02:22,070 It looks exactly as it did, which is good. 38 00:02:22,070 --> 00:02:25,090 We haven't made any changes to the function of the code. 39 00:02:25,090 --> 00:02:30,510 All we're doing is generating the catalog array from the database using pdo. 40 00:02:30,510 --> 00:02:35,730 If I click on Books in the navigation, we'll see our Books page, Movies, 41 00:02:35,730 --> 00:02:38,160 Music and our Full Catalog. 42 00:02:39,280 --> 00:02:42,520 All of those pages currently call functions that in turn 43 00:02:42,520 --> 00:02:43,530 use the catalog array. 44 00:02:44,540 --> 00:02:48,070 We were able to change the source of the data displayed on these pages 45 00:02:48,070 --> 00:02:52,230 by simply putting the results of our database query into a catalog array, 46 00:02:52,230 --> 00:02:55,450 instead of recreating that array manually. 47 00:02:55,450 --> 00:02:57,595 Just to make sure that we're using the database, 48 00:02:57,595 --> 00:03:00,318 let's change our connection string to break the connection. 49 00:03:06,144 --> 00:03:09,470 If our code is working properly, this will show an error message. 50 00:03:09,470 --> 00:03:10,620 This looks right. 51 00:03:10,620 --> 00:03:13,440 Our site is using our code that connects to the database and 52 00:03:13,440 --> 00:03:16,210 when the string is wrong, we're getting the error message. 53 00:03:16,210 --> 00:03:17,184 Let's change that back. 54 00:03:21,414 --> 00:03:22,530 And everything looks good. 55 00:03:24,310 --> 00:03:25,690 >> Before we move on. 56 00:03:25,690 --> 00:03:28,530 I want to point out that we will be improving the performance 57 00:03:28,530 --> 00:03:30,890 on some of these pages shortly. 58 00:03:30,890 --> 00:03:34,230 Currently our code retrieves all the items in the catalog. 59 00:03:34,230 --> 00:03:38,050 This makes our code do extra unnecessary work. 60 00:03:38,050 --> 00:03:41,930 For example to display only the books in the catalog. 61 00:03:41,930 --> 00:03:46,900 The code retrieves the entire catalog, books, music and movies, 62 00:03:46,900 --> 00:03:52,450 then loops through the entire catalog to display just the books. 63 00:03:52,450 --> 00:03:57,190 This type of filtering is the type of work at which databases excel. 64 00:03:57,190 --> 00:04:02,190 We'll eventually write additional queries to make our database handle that load. 65 00:04:02,190 --> 00:04:05,370 And in turn, this makes our site faster. 66 00:04:05,370 --> 00:04:09,864 Before we're done with this project, we're going to set up multiple functions and 67 00:04:09,864 --> 00:04:12,902 multiple queries to the database to pull specific data, 68 00:04:12,902 --> 00:04:14,628 instead of the entire catalog. 69 00:04:14,628 --> 00:04:16,047 But before we do that, 70 00:04:16,047 --> 00:04:21,150 let's modify the code we just pasted to make things cleaner and easier to reuse.