1 00:00:00,390 --> 00:00:04,440 In the last video we used the built-in PHP function, array_rand, 2 00:00:04,440 --> 00:00:09,490 to pull four random keys to use for our suggestions, displayed on the homepage. 3 00:00:09,490 --> 00:00:12,330 For each of our categories, books, movies, and 4 00:00:12,330 --> 00:00:17,210 music, we want to display just the items in that corresponding category. 5 00:00:17,210 --> 00:00:18,170 To do this, 6 00:00:18,170 --> 00:00:22,910 we'll create our own function that works much like the array_rand function. 7 00:00:22,910 --> 00:00:26,050 We'll pass in two parameters and return an array of keys. 8 00:00:27,510 --> 00:00:30,420 While currently, we only want to pull the category keys for 9 00:00:30,420 --> 00:00:34,710 the catalog page, we may decide to use that for something else in the future. 10 00:00:34,710 --> 00:00:35,230 Let's go ahead and 11 00:00:35,230 --> 00:00:39,360 add that to our functions.php file in our includes directory. 12 00:00:39,360 --> 00:00:41,890 Then we can simply include the functions file 13 00:00:41,890 --> 00:00:43,990 if we want to use that function later. 14 00:00:43,990 --> 00:00:45,568 Let's get coding. 15 00:00:45,568 --> 00:00:48,569 Let's go back into functions.php. 16 00:00:48,569 --> 00:00:56,729 We're going to add a second function called array_category. 17 00:00:56,729 --> 00:01:04,348 For the parameters, we need the catalog array, And the category we wish to return. 18 00:01:08,548 --> 00:01:10,048 We want to return an array. 19 00:01:10,048 --> 00:01:14,149 So let's start by creating an empty array variable named output. 20 00:01:19,349 --> 00:01:23,028 And we'll return output. 21 00:01:23,028 --> 00:01:27,244 We need to loop through our catalog array, so once again we create a foreach loop. 22 00:01:27,244 --> 00:01:32,453 foreach ($catalog as 23 00:01:32,453 --> 00:01:37,150 $id => $item). 24 00:01:37,150 --> 00:01:37,880 For each item, 25 00:01:37,880 --> 00:01:41,940 we need to check if the category is the one that matches our category parameter. 26 00:01:44,680 --> 00:01:51,030 If ($category ==, because we're comparing, 27 00:01:51,030 --> 00:01:54,790 $item["category"]). 28 00:01:59,220 --> 00:02:02,034 If you look at the data.php file, 29 00:02:02,034 --> 00:02:07,188 you'll see that the values for the category are capitalized. 30 00:02:07,188 --> 00:02:09,549 But we don't really need this to be case sensitive. 31 00:02:09,549 --> 00:02:12,234 So we're going to use another built-in function and 32 00:02:12,234 --> 00:02:14,808 convert both of these string values to lowercase. 33 00:02:18,788 --> 00:02:23,668 The function is strtolower. 34 00:02:23,668 --> 00:02:26,732 We simply pass the string value we want to convert, and 35 00:02:26,732 --> 00:02:29,408 it returns the string converted to lowercase. 36 00:02:36,648 --> 00:02:38,665 We add this to both our variables, and 37 00:02:38,665 --> 00:02:42,140 now the capitalization of the category we pass in won't matter. 38 00:02:43,650 --> 00:02:46,100 And how we've stored our variable won't matter. 39 00:02:47,470 --> 00:02:50,260 We are looking only at the letters themselves. 40 00:02:50,260 --> 00:02:53,230 If the category we passed in, matches the category of the current 41 00:02:53,230 --> 00:02:57,460 item we are looping through, we need to add the id to the output array. 42 00:03:00,280 --> 00:03:07,329 $output[] = $id;. 43 00:03:07,329 --> 00:03:10,630 Our function is ready, we just need to call it from our catalog page. 44 00:03:12,260 --> 00:03:15,945 Let's go into our index.php file, and 45 00:03:15,945 --> 00:03:19,559 copy our call to our function, as well as our foreach loop. 46 00:03:21,370 --> 00:03:24,690 Then we'll go into catalog and replace our foreach loop. 47 00:03:26,110 --> 00:03:28,370 Now let's change these values a little. 48 00:03:28,370 --> 00:03:33,260 Instead of random, we're going to be calling our category ids. 49 00:03:33,260 --> 00:03:36,208 So let's call this $categories. 50 00:03:41,428 --> 00:03:46,868 Next, we want to call our new function array_category. 51 00:03:46,868 --> 00:03:50,897 We'll still pass the catalog variable but for the second parameter, 52 00:03:50,897 --> 00:03:52,609 we need to pass the category. 53 00:03:52,609 --> 00:03:58,648 At the top of this page, we already set the section variable. 54 00:03:58,648 --> 00:04:05,128 Here, here, here, or here. 55 00:04:05,128 --> 00:04:06,268 So let's use that. 56 00:04:09,709 --> 00:04:16,928 Now when we preview our site and click on books, we see just the books. 57 00:04:16,928 --> 00:04:21,809 When we click on movies, we see just our movie items. 58 00:04:21,809 --> 00:04:26,209 And when we click on music, we see just our music items. 59 00:04:26,209 --> 00:04:30,028 That looks great, but what happens when we wanna try to view the full catalog? 60 00:04:33,808 --> 00:04:35,749 We don't see anything at all. 61 00:04:35,749 --> 00:04:37,169 That's not what we want. 62 00:04:37,169 --> 00:04:42,549 What we want is it to show all the items in our catalog on the full catalog page. 63 00:04:42,549 --> 00:04:44,428 Let's take a look at our code. 64 00:04:44,428 --> 00:04:49,110 On the catalog page, we're passing the category of books, movies, or music. 65 00:04:49,110 --> 00:04:50,620 If we haven't chosen one of these, 66 00:04:50,620 --> 00:04:54,830 we're passing the default of section which is null. 67 00:04:54,830 --> 00:04:57,820 So we need to update our function to return all keys 68 00:04:57,820 --> 00:04:59,480 if null is passed as the category. 69 00:05:00,990 --> 00:05:05,750 In functions.php, let's modify our function. 70 00:05:05,750 --> 00:05:09,764 At the top of the function, let's do a simple check. 71 00:05:09,764 --> 00:05:15,111 If ($category == null) 72 00:05:15,111 --> 00:05:19,344 we're going to return 73 00:05:19,344 --> 00:05:25,145 array_keys($catalog). 74 00:05:25,145 --> 00:05:28,140 You guessed it, it returns an array of just the keys. 75 00:05:29,270 --> 00:05:34,269 Now when we go back to our browser and refresh the page, 76 00:05:34,269 --> 00:05:38,628 we see all of our items on our full catalog page. 77 00:05:38,628 --> 00:05:41,669 Our site is finally starting to look like a real library. 78 00:05:41,669 --> 00:05:42,989 You're doing great. 79 00:05:42,989 --> 00:05:45,389 You've learned a lot about arrays and functions. 80 00:05:45,389 --> 00:05:49,687 There's just one more thing I think would be really helpful before moving on, 81 00:05:49,687 --> 00:05:50,280 sorting.