1 00:00:00,300 --> 00:00:04,610 In the last video, you learned how to grab only the items in a certain category. 2 00:00:04,610 --> 00:00:07,950 With our current data, that's only four items on a category page. 3 00:00:07,950 --> 00:00:11,044 So it doesn't really make much difference how it's sorted. 4 00:00:11,044 --> 00:00:16,018 That's easy enough to see everything, but when we look full catalog of 12 items, 5 00:00:16,018 --> 00:00:20,501 we can start to see how we could easily miss a particular item when our catalog 6 00:00:20,501 --> 00:00:22,040 expands. 7 00:00:22,040 --> 00:00:26,010 It would be much easier to find things if we could sort this by title. 8 00:00:26,010 --> 00:00:28,570 So, let's get into our function and make some modifications. 9 00:00:30,590 --> 00:00:35,200 Go back into function.php and go into the array category function. 10 00:00:37,290 --> 00:00:39,430 After we check if this item matches our category. 11 00:00:41,120 --> 00:00:43,350 We want to create a variable to use for sorting. 12 00:00:44,520 --> 00:00:46,510 We can choose any element to sort with, but 13 00:00:46,510 --> 00:00:49,110 title would seem to be the most useful. 14 00:00:49,110 --> 00:00:53,131 So we set sort = 15 00:00:53,131 --> 00:00:58,430 item "title". 16 00:00:58,430 --> 00:01:00,770 Now instead of just adding the key to the array, 17 00:01:00,770 --> 00:01:03,660 we also want to assign the value to sort. 18 00:01:04,920 --> 00:01:09,193 We change this line to be output id 19 00:01:09,193 --> 00:01:14,300 = sort. 20 00:01:14,300 --> 00:01:18,710 Before we return our output array, we need to do two things. 21 00:01:18,710 --> 00:01:24,917 One, sort the array using a built-in function 22 00:01:24,917 --> 00:01:30,880 called asort, asort($output); 23 00:01:30,880 --> 00:01:34,270 Since we changed our array to include more than just the keys, 24 00:01:34,270 --> 00:01:38,300 we want to return only the keys, like we did on null. 25 00:01:38,300 --> 00:01:40,345 So again, we use array_keys. 26 00:01:47,344 --> 00:01:50,190 Let's go back to our browser and see what this looks like on our page. 27 00:01:51,580 --> 00:01:55,980 We go into books, our book items are now sorted by title. 28 00:01:55,980 --> 00:02:03,121 Great, but you'll notice, The Clean Coder is at the end because it starts with a T. 29 00:02:03,121 --> 00:02:07,017 Typically, when items are alphabetized, the preposition a, and, 30 00:02:07,017 --> 00:02:10,590 and the, are ignored at the beginning of the title. 31 00:02:10,590 --> 00:02:11,980 Let's set up our code to work that way. 32 00:02:13,520 --> 00:02:20,170 We want to remove, or trim, any the, a, or and from the beginning of our sort. 33 00:02:20,170 --> 00:02:20,930 To do this, 34 00:02:20,930 --> 00:02:26,080 we'll use another built in function, ltrim, meaning trim from the left. 35 00:02:28,100 --> 00:02:31,310 After setting the variable for sort, let's add the following line. 36 00:02:31,310 --> 00:02:35,544 Sort equals ltrim, 37 00:02:35,544 --> 00:02:40,530 sort, T-H-E space. 38 00:02:41,880 --> 00:02:45,140 Make sure you include the space after T-H-E or 39 00:02:45,140 --> 00:02:49,880 it will trim T-H-E off anything and not just the word the. 40 00:02:49,880 --> 00:02:54,620 This means that theme would become me, and that's not what we want to happen. 41 00:02:55,940 --> 00:02:57,200 Let's copy this line twice. 42 00:02:59,605 --> 00:03:03,728 And change the to a and 43 00:03:03,728 --> 00:03:07,410 an respectively. 44 00:03:07,410 --> 00:03:12,803 Now, when we refresh our page in the browser, our books are sorted by title. 45 00:03:12,803 --> 00:03:17,202 And The Clean Coder is right next to Clean Code because the sort is ignoring The at 46 00:03:17,202 --> 00:03:18,790 the beginning of the title. 47 00:03:18,790 --> 00:03:24,910 Our Movies page and our Music page are also sorting alphabetically. 48 00:03:24,910 --> 00:03:28,630 The only page that's not sorting alphabetically yet is our full catalog. 49 00:03:29,760 --> 00:03:33,710 Let's go back into our function and fix that. 50 00:03:33,710 --> 00:03:34,510 Back in our function, 51 00:03:34,510 --> 00:03:38,270 we have it returning just an array of all the keys, if our category is null. 52 00:03:39,320 --> 00:03:41,840 Let's combine our if statements. 53 00:03:41,840 --> 00:03:48,920 Let's move this first if down to this if. 54 00:03:50,800 --> 00:03:53,860 Then we'll combine it with an or. 55 00:03:55,320 --> 00:03:59,070 Now our function will sort the items that match our category, or 56 00:03:59,070 --> 00:04:02,570 sort all the items if we pass a null category. 57 00:04:04,060 --> 00:04:09,230 Before we go, let's change our h1 tag to include the link to the full catalog. 58 00:04:09,230 --> 00:04:11,550 We only wanna do this if we're on a category page. 59 00:04:12,780 --> 00:04:14,420 So we'll start by adding a conditional. 60 00:04:16,808 --> 00:04:17,971 Up here with our page title. 61 00:04:17,971 --> 00:04:22,447 We'll first start with if 62 00:04:22,447 --> 00:04:27,130 section is not equal null. 63 00:04:31,540 --> 00:04:33,010 Going to do something. 64 00:04:34,880 --> 00:04:39,070 Pay special attention to the exclamation point which is our negation or 65 00:04:39,070 --> 00:04:40,560 not operator. 66 00:04:40,560 --> 00:04:43,400 It can be confusing when you start working with negatives, but 67 00:04:43,400 --> 00:04:45,990 it can also make things a lot easier. 68 00:04:45,990 --> 00:04:50,690 One value not true versus multiple values that are true. 69 00:04:50,690 --> 00:04:54,560 If our section variable is null then we're on our full catalog. 70 00:04:54,560 --> 00:04:56,760 And we don't need to show that link. 71 00:04:56,760 --> 00:05:00,470 But we are checking to see if our section variable is not null. 72 00:05:00,470 --> 00:05:03,390 So we can print an extra line that links to our full catalog. 73 00:05:07,687 --> 00:05:12,487 We're going to use 74 00:05:12,487 --> 00:05:17,587 a special character 75 00:05:17,587 --> 00:05:23,590 to separate our title. 76 00:05:25,272 --> 00:05:28,810 & gt;, 77 00:05:28,810 --> 00:05:34,310 this is a greater than sign but it keeps it from being confused with our HTML. 78 00:05:34,310 --> 00:05:38,800 Whether our section variable is null or not, we still want to keep our page title. 79 00:05:38,800 --> 00:05:43,010 Now when we preview our page, we can click through our categories and 80 00:05:43,010 --> 00:05:44,150 back to our full catalog. 81 00:05:45,340 --> 00:05:47,420 Our Catalog page is ready to go. 82 00:05:47,420 --> 00:05:51,940 And you've learned a lot about utilizing arrays and the built-in array functions. 83 00:05:51,940 --> 00:05:55,970 There are several other functions associated with sorting and arrays. 84 00:05:55,970 --> 00:06:00,950 Including reverse order and sorting the array keys instead of the values. 85 00:06:00,950 --> 00:06:04,920 Go back into the array functions in the PHP docs and give some of them a try.