1 00:00:00,380 --> 00:00:05,350 In our last video we created our own function called get item HTML. 2 00:00:05,350 --> 00:00:09,670 This allows us to get HTML for a specific item for our list. 3 00:00:09,670 --> 00:00:13,430 Currently our home page loops through each of the items in the catalog. 4 00:00:13,430 --> 00:00:15,290 Just like the catalog page. 5 00:00:15,290 --> 00:00:18,200 Instead of the entire catalog we want to pull four 6 00:00:18,200 --> 00:00:20,860 random products to suggest to our user. 7 00:00:20,860 --> 00:00:23,630 To do that, we're gonna use a built-in PHP function. 8 00:00:25,240 --> 00:00:27,910 PHP offers an number of built-in functions. 9 00:00:27,910 --> 00:00:32,400 The PHP Docs are a great place to look for things that you want to do, and 10 00:00:32,400 --> 00:00:36,740 it gives you the exact syntax that you need to use to make these functions work. 11 00:00:36,740 --> 00:00:37,330 Let's check it out. 12 00:00:38,760 --> 00:00:41,560 As you can see there are quite a few functions available, and 13 00:00:41,560 --> 00:00:44,305 I'd encourage you to play around with them yourself. 14 00:00:44,305 --> 00:00:46,360 We'll use a couple more of these built in functions later on. 15 00:00:47,410 --> 00:00:54,180 For now, we're looking for a function called array_rand. 16 00:00:54,180 --> 00:00:56,980 Clicking on that function gives you the details of that function and 17 00:00:56,980 --> 00:00:58,190 how to use it. 18 00:00:58,190 --> 00:01:00,010 Just like the function we created, 19 00:01:00,010 --> 00:01:03,880 the array_rand function takes two parameters and returns a value. 20 00:01:04,930 --> 00:01:07,540 The first parameter is the array and 21 00:01:07,540 --> 00:01:10,720 the second parameter is the number of elements we want to grab. 22 00:01:11,970 --> 00:01:17,270 Looking at return values, we see that if only one item is returned, 23 00:01:17,270 --> 00:01:20,030 it returns that single key element. 24 00:01:20,030 --> 00:01:24,050 If multiple items are returned, it puts the key into an array. 25 00:01:24,050 --> 00:01:25,490 Let's try this out in our own code. 26 00:01:25,490 --> 00:01:28,206 Let's go into index.php. 27 00:01:28,206 --> 00:01:30,096 We're going to create a new. 28 00:01:32,336 --> 00:01:33,996 Random variable. 29 00:01:37,056 --> 00:01:42,256 We'll assign this the return 30 00:01:42,256 --> 00:01:46,417 value of array_rand. 31 00:01:46,417 --> 00:01:53,010 We pass in our catalog and the number four is the number of items we want to return. 32 00:01:55,620 --> 00:02:00,280 Let's do a var_dump to check out these results. 33 00:02:08,468 --> 00:02:11,570 The var_dump command will display all the possible information about 34 00:02:11,570 --> 00:02:13,130 a particular variable on the screen. 35 00:02:14,150 --> 00:02:19,679 You can see in the browser that the new random variable is an array, 36 00:02:19,679 --> 00:02:24,223 with four elements, that contain the random keys from 37 00:02:24,223 --> 00:02:29,186 our catalog array, 102, 103, 104 and 303. 38 00:02:29,186 --> 00:02:32,175 To use this we're going to use our for each loop again but 39 00:02:32,175 --> 00:02:36,740 this time we're going to just grab each key from our random array. 40 00:02:36,740 --> 00:02:42,100 Let's go back into index.php, let's remove our var_dump. 41 00:02:42,100 --> 00:02:50,470 And instead of catalog we want to just do random as id. 42 00:02:50,470 --> 00:02:56,675 Then in our get_item_html, we'll pass the id, but instead of item, 43 00:02:56,675 --> 00:03:01,777 we're gonna use our catalogue, and we'll pass in our id. 44 00:03:04,956 --> 00:03:10,221 Now, when we save this page and refresh it in our browser, 45 00:03:10,221 --> 00:03:14,920 we'll see that four items are selected. 46 00:03:14,920 --> 00:03:18,880 When we refresh again, we see another random four items. 47 00:03:20,660 --> 00:03:23,890 Great job, you've used the PHP docs to learn about and 48 00:03:23,890 --> 00:03:28,820 use built-in functions that make our job as programmers a lot easier. 49 00:03:28,820 --> 00:03:33,000 In the next video, we'll discuss how to pull specific items from our catalog. 50 00:03:33,000 --> 00:03:36,079 In this case the category we specify.