1 00:00:00,540 --> 00:00:04,530 We have a collection of all our recipes in the cookbook object. 2 00:00:04,530 --> 00:00:08,450 And we're able to grab a shopping list of all our recipes. 3 00:00:08,450 --> 00:00:11,230 But there are more recipes than I'm going to make in a week. 4 00:00:11,230 --> 00:00:15,050 So I don't want to go shopping for all the recipes at once. 5 00:00:15,050 --> 00:00:18,510 Something that would make this much more useful is the ability to have 6 00:00:18,510 --> 00:00:20,260 a weekly meal plan. 7 00:00:20,260 --> 00:00:23,420 Then I could use the shopping list from that collection and 8 00:00:23,420 --> 00:00:24,830 actually purchase what I need. 9 00:00:25,890 --> 00:00:29,510 We used our breakfast tag to create a second collection object 10 00:00:29,510 --> 00:00:32,310 that pulled recipes based on tag. 11 00:00:32,310 --> 00:00:35,230 Since we want to choose the recipes from our meal plan, 12 00:00:35,230 --> 00:00:39,140 we need to be able to specify an individual recipe. 13 00:00:39,140 --> 00:00:43,820 We could select the recipe based on the title, but that can get long and 14 00:00:43,820 --> 00:00:44,730 be easy to mistype. 15 00:00:44,730 --> 00:00:49,530 It would be much easier if we could just specify an id. 16 00:00:49,530 --> 00:00:50,790 Let's make that magic happen. 17 00:00:52,500 --> 00:00:55,590 I want to change the display of the recipe list, so 18 00:00:55,590 --> 00:00:57,780 we want to change this in the render class. 19 00:00:58,780 --> 00:01:02,910 Let's update the list recipes method so it shows the array key for 20 00:01:02,910 --> 00:01:04,070 that recipe as well. 21 00:01:05,200 --> 00:01:08,320 Implode only pulls the values from an array, so 22 00:01:08,320 --> 00:01:11,710 we'll need to loop through the array and use the value and the key. 23 00:01:13,045 --> 00:01:18,100 asort does maintain the key association, so we can sort first. 24 00:01:18,100 --> 00:01:20,140 Then we need to initialize the output. 25 00:01:23,533 --> 00:01:25,287 Now I can use our foreach loop. 26 00:01:29,931 --> 00:01:34,312 We use, $key => and 27 00:01:34,312 --> 00:01:39,622 then our value, $title. 28 00:01:39,622 --> 00:01:43,840 Now we can use the key and value for our recipe title. 29 00:01:43,840 --> 00:01:47,830 Because the title length may vary, I think it will look neater and 30 00:01:47,830 --> 00:01:52,980 be easier to read with the key in square brackets at the beginning of each line. 31 00:01:52,980 --> 00:01:54,022 So let's concatenate. 32 00:01:57,323 --> 00:02:00,930 We'll add $key and then $title. 33 00:02:04,340 --> 00:02:08,270 Speaking of lines, I need to output a newline character. 34 00:02:08,270 --> 00:02:13,030 But I only want the new lines between each of the recipe titles, 35 00:02:13,030 --> 00:02:15,520 just like I had for the implode. 36 00:02:15,520 --> 00:02:18,990 I don't want the new line to be at the beginning or the end of our list. 37 00:02:20,610 --> 00:02:22,811 I can do that by adding a conditional. 38 00:02:25,783 --> 00:02:31,607 If our $output is not equal to an empty string. 39 00:02:33,718 --> 00:02:36,125 Then I want to add the new line character. 40 00:02:41,229 --> 00:02:43,133 Now I need to return the $output. 41 00:02:46,760 --> 00:02:50,035 Let's go back to our cookbook and take a look at our list now. 42 00:02:51,731 --> 00:02:54,310 I'm going to comment out these shopping list lines. 43 00:02:56,970 --> 00:03:02,236 And then I'm going to echo Render our list recipes. 44 00:03:05,020 --> 00:03:07,137 And then I'm going to pass it our $cookbook. 45 00:03:09,188 --> 00:03:11,672 With getRecipeTitles. 46 00:03:15,045 --> 00:03:16,588 Now let's run this file. 47 00:03:21,838 --> 00:03:26,495 It looks like it's separated by a quote instead of a new line character. 48 00:03:26,495 --> 00:03:27,610 Let's check that out. 49 00:03:33,378 --> 00:03:34,815 Now let's run the script again. 50 00:03:36,590 --> 00:03:38,430 Much easier. 51 00:03:38,430 --> 00:03:42,290 Now we see an alphabetized list of recipes with the key for 52 00:03:42,290 --> 00:03:44,680 the array at the beginning of each line. 53 00:03:45,970 --> 00:03:50,680 Now that I know that my Belgian waffle recipe is number two, 54 00:03:50,680 --> 00:03:53,030 let's take a look at how to pull that recipe. 55 00:03:54,330 --> 00:03:56,567 Let's go back to our recipe collection. 56 00:03:58,212 --> 00:04:00,561 We're going to add a new method. 57 00:04:05,500 --> 00:04:08,626 We'll call this new method filterById. 58 00:04:11,915 --> 00:04:13,810 And we'll pass it the $id. 59 00:04:15,277 --> 00:04:23,180 Then we need to return $this -> recipes with that $id. 60 00:04:24,860 --> 00:04:26,440 Let's go back to our cookbook and 61 00:04:26,440 --> 00:04:29,700 see how we would use that to display the Belgian waffle recipe. 62 00:04:32,070 --> 00:04:33,340 Let's comment out this line. 63 00:04:33,340 --> 00:04:37,382 Now we'll go back to our display. 64 00:04:37,382 --> 00:04:42,490 Instead of Belgian waffles, I'm going 65 00:04:42,490 --> 00:04:47,908 to use $cookbook, and then filterById. 66 00:04:47,908 --> 00:04:50,890 And we'll pass it the $id of 2. 67 00:04:50,890 --> 00:04:52,420 And let's uncomment this line. 68 00:04:53,970 --> 00:04:55,104 Then we can run our script. 69 00:04:58,687 --> 00:05:02,700 Great, now we see our Belgian waffles by Alena Holligan. 70 00:05:02,700 --> 00:05:06,588 Now we can use this to add the recipes we want to our meal plan. 71 00:05:09,290 --> 00:05:13,287 First, let's display our recipe list again. 72 00:05:18,070 --> 00:05:22,396 I want to add 2 and 73 00:05:22,396 --> 00:05:27,014 3 and 6 and 16. 74 00:05:33,830 --> 00:05:37,899 So, let's create a $week1. 75 00:05:37,899 --> 00:05:41,341 And this will be a new RecipeCollection. 76 00:05:43,493 --> 00:05:48,713 We'll name this Meal Plan: Week 1. 77 00:05:51,650 --> 00:05:54,024 Now we can add the recipes. 78 00:06:08,446 --> 00:06:13,102 We want item 2, item 3, 79 00:06:13,102 --> 00:06:17,131 item 6 and item 16. 80 00:06:20,397 --> 00:06:23,585 Now let's list those recipes. 81 00:06:23,585 --> 00:06:24,588 Week one. 82 00:06:29,683 --> 00:06:32,880 Great, now we see all four recipes. 83 00:06:32,880 --> 00:06:34,867 But let's display the shopping list. 84 00:06:48,733 --> 00:06:50,530 Let's view this in the browser again. 85 00:06:53,580 --> 00:06:56,200 Great, now we have our shopping list of ingredients and 86 00:06:56,200 --> 00:06:57,350 we're ready to go shopping.