Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Using a built in PHP function, we’ll choose four random items to suggest on the home page.
Increase Randomness
When using array_rand, the resulting array of keys is NOT shuffled, the books will always come before movies, which will come before music. If you want to randomize these even further you could use the shuffle function as well.
Example:
$random =array_rand($catalog,4); shuffle($random);
-
0:00
In our last video we created our own function called get item HTML.
-
0:05
This allows us to get HTML for a specific item for our list.
-
0:09
Currently our home page loops through each of the items in the catalog.
-
0:13
Just like the catalog page.
-
0:15
Instead of the entire catalog we want to pull four
-
0:18
random products to suggest to our user.
-
0:20
To do that, we're gonna use a built-in PHP function.
-
0:25
PHP offers an number of built-in functions.
-
0:27
The PHP Docs are a great place to look for things that you want to do, and
-
0:32
it gives you the exact syntax that you need to use to make these functions work.
-
0:36
Let's check it out.
-
0:38
As you can see there are quite a few functions available, and
-
0:41
I'd encourage you to play around with them yourself.
-
0:44
We'll use a couple more of these built in functions later on.
-
0:47
For now, we're looking for a function called array_rand.
-
0:54
Clicking on that function gives you the details of that function and
-
0:56
how to use it.
-
0:58
Just like the function we created,
-
1:00
the array_rand function takes two parameters and returns a value.
-
1:04
The first parameter is the array and
-
1:07
the second parameter is the number of elements we want to grab.
-
1:11
Looking at return values, we see that if only one item is returned,
-
1:17
it returns that single key element.
-
1:20
If multiple items are returned, it puts the key into an array.
-
1:24
Let's try this out in our own code.
-
1:25
Let's go into index.php.
-
1:28
We're going to create a new.
-
1:32
Random variable.
-
1:37
We'll assign this the return
-
1:42
value of array_rand.
-
1:46
We pass in our catalog and the number four is the number of items we want to return.
-
1:55
Let's do a var_dump to check out these results.
-
2:08
The var_dump command will display all the possible information about
-
2:11
a particular variable on the screen.
-
2:14
You can see in the browser that the new random variable is an array,
-
2:19
with four elements, that contain the random keys from
-
2:24
our catalog array, 102, 103, 104 and 303.
-
2:29
To use this we're going to use our for each loop again but
-
2:32
this time we're going to just grab each key from our random array.
-
2:36
Let's go back into index.php, let's remove our var_dump.
-
2:42
And instead of catalog we want to just do random as id.
-
2:50
Then in our get_item_html, we'll pass the id, but instead of item,
-
2:56
we're gonna use our catalogue, and we'll pass in our id.
-
3:04
Now, when we save this page and refresh it in our browser,
-
3:10
we'll see that four items are selected.
-
3:14
When we refresh again, we see another random four items.
-
3:20
Great job, you've used the PHP docs to learn about and
-
3:23
use built-in functions that make our job as programmers a lot easier.
-
3:28
In the next video, we'll discuss how to pull specific items from our catalog.
-
3:33
In this case the category we specify.
You need to sign up for Treehouse in order to download course files.
Sign up