1 00:00:00,440 --> 00:00:02,929 Back in Workspaces, open functions.php and 2 00:00:02,929 --> 00:00:05,762 scroll back to our single_item_array function. 3 00:00:05,762 --> 00:00:10,000 Our single_item_array function has one query right now 4 00:00:10,000 --> 00:00:14,040 that retrieves data from the Media table, Genre table and 5 00:00:14,040 --> 00:00:19,130 optionally from the Books table, but only if that data exists. 6 00:00:19,130 --> 00:00:23,310 Let's add another query that will retrieve the people associated with this item. 7 00:00:24,550 --> 00:00:27,960 We'll need to add the people with their roles to our item array, 8 00:00:27,960 --> 00:00:31,220 in the same format that they were in before. 9 00:00:31,220 --> 00:00:35,670 Let's take a look at our data.php file to review what the item array 10 00:00:35,670 --> 00:00:36,950 used to look like. 11 00:00:36,950 --> 00:00:44,840 Inside each item we had an array element for each role, using the role as the key. 12 00:00:44,840 --> 00:00:49,880 Inside that element, we had an array with one element for each person. 13 00:00:49,880 --> 00:00:55,760 We didn't specify the key, so they must have been indexed zero, one and so on. 14 00:00:55,760 --> 00:01:01,020 Let's go back to our functions.php file, and the single item array function. 15 00:01:01,020 --> 00:01:02,960 We can close this file. 16 00:01:02,960 --> 00:01:05,160 We want to create the same structure here, so 17 00:01:05,160 --> 00:01:09,160 that we don't have to modify the code in our details page very much. 18 00:01:09,160 --> 00:01:11,020 We get the main item details here. 19 00:01:12,020 --> 00:01:15,210 If we have an item at this point, we also want to look up people. 20 00:01:16,290 --> 00:01:20,550 But if no item matches the id, there's no need to look any further. 21 00:01:20,550 --> 00:01:23,693 I'm going to change this variable from catalog to item, 22 00:01:23,693 --> 00:01:25,860 since we're only returning one item. 23 00:01:31,300 --> 00:01:33,550 If our item variable is a Boolean false, 24 00:01:33,550 --> 00:01:38,340 we could actually just return that false without proceeding any further. 25 00:01:38,340 --> 00:01:42,800 We can use a conditional to check for that, just like we do in the details page. 26 00:01:42,800 --> 00:01:48,668 if (empty($item)) 27 00:01:48,668 --> 00:01:53,150 return $item;. 28 00:01:53,150 --> 00:01:54,750 This is called an early return. 29 00:01:55,890 --> 00:01:58,320 Once you have everything you need from the function, 30 00:01:58,320 --> 00:02:01,410 you can put a return here to return that value. 31 00:02:01,410 --> 00:02:04,580 You can have multiple return statements in a function. 32 00:02:04,580 --> 00:02:08,110 And once one is encountered, then the function returns that value and 33 00:02:08,110 --> 00:02:10,620 stops executing anymore code. 34 00:02:10,620 --> 00:02:14,690 You'll often see multiple return statements nested in conditionals, so 35 00:02:14,690 --> 00:02:17,160 that only one of them will be executed. 36 00:02:17,160 --> 00:02:22,030 If a conditional only has one command you don't technically need the curly brackets 37 00:02:22,030 --> 00:02:23,590 or hard returns. 38 00:02:23,590 --> 00:02:26,620 This single line conditional is perfectly valid. 39 00:02:26,620 --> 00:02:29,560 I don't usually like to write conditionals like this, because it can be 40 00:02:29,560 --> 00:02:34,710 confusing to scan and see exactly what code is contained within the conditional. 41 00:02:34,710 --> 00:02:38,200 But an early return with only one command inside the conditional, 42 00:02:38,200 --> 00:02:39,800 is a perfect case to use this style. 43 00:02:40,840 --> 00:02:46,050 If no item matches the id, we do an early return with a Boolean false. 44 00:02:46,050 --> 00:02:47,580 If we do have an item though, 45 00:02:47,580 --> 00:02:52,100 we need to add the people to the item array along with their roles. 46 00:02:52,100 --> 00:02:55,930 So we'll want to query the database for a list of people and roles. 47 00:02:55,930 --> 00:02:59,990 The code to run this query will look pretty similar to what we've seen before. 48 00:02:59,990 --> 00:03:01,841 So, let's copy our try catch block from above. 49 00:03:10,666 --> 00:03:13,026 We have already included the database file, so 50 00:03:13,026 --> 00:03:15,365 we can keep using the db variable. 51 00:03:15,365 --> 00:03:18,370 We'll be looking up people using our media people table, 52 00:03:18,370 --> 00:03:20,955 and the media_id that we've received as input. 53 00:03:20,955 --> 00:03:23,640 So we'll need a prepared statement again. 54 00:03:23,640 --> 00:03:25,972 We only need to select two columns. 55 00:03:29,645 --> 00:03:33,570 Fullname and role. 56 00:03:33,570 --> 00:03:37,961 We first select from our Media_People table. 57 00:03:37,961 --> 00:03:39,221 Then we'll JOIN our people table. 58 00:03:42,951 --> 00:03:50,831 We'll join people on media, people-id. 59 00:03:50,831 --> 00:03:53,851 We'll join this to, People. 60 00:03:56,811 --> 00:03:58,900 People_id. 61 00:03:58,900 --> 00:04:01,545 We can remove our LEFT OUTER JOIN, but keep the WHERE. 62 00:04:03,900 --> 00:04:08,942 We'll need to change the table name in our WHERE clause. 63 00:04:08,942 --> 00:04:11,283 Media_People. 64 00:04:11,283 --> 00:04:14,616 We'll use the same placeholder for the id as we did above, 65 00:04:14,616 --> 00:04:17,600 so our buying statement stays the same. 66 00:04:17,600 --> 00:04:20,300 And of course, we execute the query. 67 00:04:20,300 --> 00:04:24,560 This will retrieve all the people linked to this particular item, and 68 00:04:24,560 --> 00:04:26,220 put them in the results object. 69 00:04:27,220 --> 00:04:29,230 Next we need to format these results.