Creating multiple random items, instead of just one, is pretty easy. All we need to do is use the built in PHP function array_rand() to grab multiple random entries from our array.
For this example, we'll use some of my affiliates' minibanners for the objects that we'll be randomizing. First, let's build the array.
<?php
|
|
Now that we have our array, we can grab some of them out of the array randomly. The array_rand() function takes two parameters. The second of which is optional. The first parameter is your array, in this case we're using $affiliates. The second parameter is how many random items you want to pull from the array.
If you leave out the second parameter, the array_rand function will return 1 random item from your array.
Hey!? Why didn't you just use array_rand() and leave the second parameter out to grab one random item, in your quotes example?
I wanted to show you to more methods of grabbing random items, rather than just showing you how to use one function.
Now, let's use array_rand to pull two random items from the $affilates array. Here's an example:
<?php
|
|
The code above will print somthing like this:
Array ( [0] => 0 [1] => 4 )
These are two random keys from our $affiliates array. Now that we have these, we can loop through them and use them to echo out two random images from our $affiliates array. Like this:
<?php
|
|
What that does is takes the two random affiliate keys and loops through them, each time through the loop it echos out an image tag, and sets it's src to our affiliate mini banners.
Put it all together and you come up with this:
<?php
|
|
That's it! There's your basic random content script(s). Pretty easy, huh?
Try customizing either of the examples provided in this tutorial.
See if you can improve upon them. 
Hope this tutorial has been helpful to you.
| Discuss Tutorial: Randomizing Content | 14 Comments |

.



