1 00:00:00,590 --> 00:00:03,480 Let's put together that skills that you've learned in this course 2 00:00:03,480 --> 00:00:06,110 to add functionality to our to do app. 3 00:00:06,110 --> 00:00:11,120 We'll start by displaying only completed or incomplete tasks, then we'll 4 00:00:11,120 --> 00:00:16,609 explore sorting multi dimensional arrays, using a particular inner key element. 5 00:00:16,609 --> 00:00:20,984 For our to do list, this would be the title, priority, due date or 6 00:00:20,984 --> 00:00:23,180 completion status. 7 00:00:23,180 --> 00:00:27,970 We'll start by displaying only complete or incomplete tasks. 8 00:00:27,970 --> 00:00:31,960 Within the table, we are looping through each item in our array. 9 00:00:33,590 --> 00:00:37,380 We then check the complete status to show yes or 10 00:00:37,380 --> 00:00:41,300 no, whether the item is complete or not. 11 00:00:41,300 --> 00:00:45,098 We could use this same conditional at the top of our for each loop, 12 00:00:45,098 --> 00:00:47,600 wrapping the entire row in a conditional. 13 00:00:58,955 --> 00:01:03,000 When we refresh the browser we see that only the complete items show. 14 00:01:05,670 --> 00:01:08,050 We're going to be adding more filters to this data so 15 00:01:08,050 --> 00:01:10,332 let's remove the conditional from the display. 16 00:01:14,150 --> 00:01:15,429 Now we'll go up to the top of the page. 17 00:01:17,053 --> 00:01:19,907 For each of these new filter features, 18 00:01:19,907 --> 00:01:24,242 we're going to create a list of keys in a particular order. 19 00:01:24,242 --> 00:01:29,750 We will then use those keys to access particular items in our list. 20 00:01:30,750 --> 00:01:32,793 Let's create a new array named filter. 21 00:01:36,957 --> 00:01:40,638 We're going to loop through each item in the list array, and 22 00:01:40,638 --> 00:01:43,180 I want both the key and the value. 23 00:01:43,180 --> 00:01:47,140 So we can use the top for each loop that we set up last time. 24 00:01:47,140 --> 00:01:49,130 Let's add the filter for complete status. 25 00:01:50,200 --> 00:01:56,624 We add a conditional, if item, Complete, 26 00:01:59,927 --> 00:02:04,160 And if the item is complete, we add the key to the filter array. 27 00:02:11,763 --> 00:02:16,130 We're only adding the key to our filter, but I wanna see the details. 28 00:02:16,130 --> 00:02:18,916 So I'm going to do a var dump of both the key and the item. 29 00:02:20,730 --> 00:02:24,090 Var_dump($key,$item);. 30 00:02:26,140 --> 00:02:27,874 Let's surround the dump with pre tags so 31 00:02:27,874 --> 00:02:31,143 that it will display with line breaks in a browser and make it easier to read. 32 00:02:40,362 --> 00:02:43,498 After the loop let's var dump the filter array and 33 00:02:43,498 --> 00:02:46,120 the list array as well to see what we have. 34 00:02:48,200 --> 00:02:50,965 We'll copy this whole dump with the pre tags. 35 00:02:57,752 --> 00:03:02,704 And we'll dump filter, and list. 36 00:03:02,704 --> 00:03:04,770 Now let's take a look at this in the browser again. 37 00:03:08,932 --> 00:03:13,842 The value from our filter array corresponds to the key 38 00:03:13,842 --> 00:03:17,080 from the dump in the loop. 39 00:03:17,080 --> 00:03:24,520 There is only a single key, item dump, and only a single item in the filter array. 40 00:03:24,520 --> 00:03:29,742 This is because there is only a single complete item in our list array. 41 00:03:29,742 --> 00:03:34,560 If we scroll down we can also see this item in our list array. 42 00:03:38,123 --> 00:03:40,633 Now that we know what our array looks like, 43 00:03:40,633 --> 00:03:45,037 let's remove this dump from our loop and we'll comment out the dump here. 44 00:03:48,586 --> 00:03:54,070 The filter array contains only the item keys we wish to show. 45 00:03:54,070 --> 00:03:58,777 So we can use the values from our filter array to show the items 46 00:03:58,777 --> 00:04:00,450 from our list array. 47 00:04:01,720 --> 00:04:04,180 Instead of looking through the list array for 48 00:04:04,180 --> 00:04:07,000 our table, we'll loop through the filter array. 49 00:04:08,020 --> 00:04:12,910 In this way we will use only the items in the filter array. 50 00:04:12,910 --> 00:04:16,730 I'm going to rename the item variable to id. 51 00:04:16,730 --> 00:04:21,125 This is because it identifies the list item we're going to use. 52 00:04:21,125 --> 00:04:26,610 The variable item no longer exists because we changed it to the id. 53 00:04:26,610 --> 00:04:30,316 Instead of using the item variable in our display, 54 00:04:30,316 --> 00:04:35,156 we're going to reference the list directly through its index id. 55 00:04:35,156 --> 00:04:41,240 So, we'll use list and then we use the id from our loop. 56 00:04:43,940 --> 00:04:47,930 Let's change item for list id for each field. 57 00:04:52,960 --> 00:04:55,720 Making sure we don't forget the complete status. 58 00:04:57,120 --> 00:04:57,930 Now let's go back to the browser. 59 00:05:01,018 --> 00:05:05,882 The table shows only the items that are complete, this is nice. 60 00:05:05,882 --> 00:05:10,150 But we probably want to be able to show incomplete items. 61 00:05:11,792 --> 00:05:14,781 We could change our conditional to look for 62 00:05:14,781 --> 00:05:19,690 a false value instead, by putting our negation operator right here. 63 00:05:20,890 --> 00:05:26,050 But let's make it easy to change between complete and incomplete statuses. 64 00:05:26,050 --> 00:05:31,459 Instead of hard coding the status into our conditional, 65 00:05:31,459 --> 00:05:38,230 we're going to check if the item complete equals the variable status. 66 00:05:38,230 --> 00:05:43,309 We can then use a variable status to represent a true-false value, 67 00:05:43,309 --> 00:05:46,178 we'll add this to the top of our file. 68 00:05:46,178 --> 00:05:51,545 $status = false, this will allow us to make changes 69 00:05:51,545 --> 00:05:57,641 to what our script would display right at the top of the file. 70 00:05:57,641 --> 00:06:00,949 Our status is set to false, so, let's go back to the browser. 71 00:06:03,579 --> 00:06:06,110 Now we see our list of incomplete items. 72 00:06:07,520 --> 00:06:09,602 Let's change status to true. 73 00:06:13,720 --> 00:06:16,774 And this time we see a list where our status is complete. 74 00:06:16,774 --> 00:06:20,650 Finally, I still may want to see all items. 75 00:06:20,650 --> 00:06:23,833 So let's add the ability to set the status equal to all. 76 00:06:28,070 --> 00:06:31,720 Because we're using the filter array for the display, 77 00:06:31,720 --> 00:06:37,650 I named my filter array to be set, so I can't just skip this top loop entirely. 78 00:06:37,650 --> 00:06:39,785 Instead, I'm going to use another conditional. 79 00:06:42,620 --> 00:06:47,406 To check if $status = all. 80 00:06:50,992 --> 00:06:54,080 And this is going to be an either or condition. 81 00:06:54,080 --> 00:06:56,285 Either the status should be all or 82 00:06:56,285 --> 00:06:59,520 the status should equal the status of complete. 83 00:07:01,220 --> 00:07:06,186 In PHP, when a value comparison is made with one of the elements 84 00:07:06,186 --> 00:07:11,069 being a boolean value each element is evaluated as a boolean. 85 00:07:11,069 --> 00:07:18,253 Let me show you what this means in practice, we'll use this var_dump. 86 00:07:23,859 --> 00:07:31,200 I'm going to dump the status and the boolean value of the string all. 87 00:07:33,533 --> 00:07:39,631 Then I will also dump the value of the comparison of $status = 'all'. 88 00:07:42,530 --> 00:07:45,065 Let's go back to the browser with our status equal to all. 89 00:07:47,142 --> 00:07:50,902 The status variable equals the string all, but 90 00:07:50,902 --> 00:07:56,072 the boolean value of the string all is true, because any string, 91 00:07:56,072 --> 00:08:00,470 besides an empty string, evaluates to true. 92 00:08:00,470 --> 00:08:05,431 Finally, the value of the string's status equals the string all, so 93 00:08:05,431 --> 00:08:07,204 this evaluates to true. 94 00:08:07,204 --> 00:08:13,350 For this comparison, a double equals works, and we see all items. 95 00:08:13,350 --> 00:08:15,133 Let's change the status variable to false. 96 00:08:24,150 --> 00:08:27,341 Now we see that our status is false, but 97 00:08:27,341 --> 00:08:31,420 the boolean value of the string all is still true. 98 00:08:31,420 --> 00:08:35,715 So the comparison of the value of the boolean status to 99 00:08:35,715 --> 00:08:39,065 the value of the string all equals false. 100 00:08:39,065 --> 00:08:41,837 Once again, the double equals works, and 101 00:08:41,837 --> 00:08:46,080 we see only the incomplete items, but we haven't checked true. 102 00:08:47,520 --> 00:08:49,533 Let's change the status variable to true. 103 00:08:54,800 --> 00:08:59,262 This time, we see that our status is equal to true, and 104 00:08:59,262 --> 00:09:02,663 the boolean value of all is equal to true. 105 00:09:02,663 --> 00:09:09,155 So comparing the value of status to the boolean value of all equals true. 106 00:09:09,155 --> 00:09:15,080 This means that instead of showing only the complete items, we see all items. 107 00:09:16,140 --> 00:09:21,000 Instead of comparing only the value, we need to make sure that this is identically 108 00:09:21,000 --> 00:09:26,710 matching both the type and the value using a triple equals. 109 00:09:26,710 --> 00:09:31,655 Let's change both our conditional and our dump to the triple equal. 110 00:09:35,910 --> 00:09:39,771 Now I see, that although the status is true and 111 00:09:39,771 --> 00:09:46,109 the boolean value of all is true, the types boolean and string do not match. 112 00:09:46,109 --> 00:09:51,780 So my comparison is false, and we see only the complete items that we want. 113 00:09:51,780 --> 00:09:53,210 Let's check this with false again. 114 00:10:02,965 --> 00:10:06,000 We once again see only incomplete items. 115 00:10:07,799 --> 00:10:09,673 And finally let's check all again. 116 00:10:13,706 --> 00:10:16,260 And once again, we see all items.