Navigation
Poll
Would you be an active poster if Ron's Guide had a message board?


Total Votes: 62
Comments: 18 — View
Past pollsPoll idea?
Rate Ron's Guide
Rate our resource at Bigwebmaster.com
Randomizing Content
Learn how to create random content to help liven up your static web pages.

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

// build an array of affiliate minibanner images
$affiliates = array(
        
'dexxaboy.gif',
        
'ssdesigns.gif',
        
'moonorphans.gif',
        
'customgfx.gif',
        
'bobbyhensley.gif'
);

?>

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

// Grab two random affiliates
$rand_affiliates array_rand($affiliates2);

// print the result (just to make sure it's working)
print_r($rand_affiliates);

?>

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

foreach($rand_affiliates as $key) {
echo 
'<img src="images/'.$affiliates[$key].'" /><br />';
}

?>

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

// build an array of affiliate minibanner images
$affiliates = array(
        
'dexxmini.gif',
        
'avtarmini.gif',
        
'ccxmini.gif',
        
'bimini.gif',
        
'na3kmini.gif'
); 

// Grab two random affiliates
$rand_affiliates array_rand($affiliates2);

// This was just for testing, so let's comment it out
// print_r($rand_affiliates); 

foreach($rand_affiliates as $key) {
echo 
'<img src="images/'.$affiliates[$key].'" /><br />';
}


?>

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. Smile

Hope this tutorial has been helpful to you.

« Previous [ 1 2 ] Next »
Discuss Tutorial: Randomizing Content 14 Comments
Comment by Ron on Aug 15, 2004, 8:26 am
Please post questions or comments about this tutorial below. Smile
Comment by Stella Artois on Sep 1, 2004, 4:01 pm
Nice tutorial Ron: I found it hard to find out this stuff when I needed it: but it all seems so siimple once you've explained it Tongue.
Comment by Ron on Sep 19, 2004, 6:55 pm
Thanks Stella, glad it helped Smile
Comment by David on Sep 21, 2004, 1:08 pm
Thanks for the tut ron, oh and by the way, I find it amusing that you have my minibanner as one of the minibanners in the tutorials Laughing
Comment by Virus on Oct 20, 2004, 10:50 pm
Great one. But, how can I get the random images to link somewhere? Better yet, how can I make a random image map?


Thanks for your help. Smile
Comment by Tim on Dec 23, 2004, 4:56 am
Great tutorial, I am utilizing it for an "Artist Spotlight" section of my site Smile
Comment by Ron on Dec 23, 2004, 6:35 am
Excellent, I'm glad it helped Smile
Comment by Neeta Thakore on Jan 31, 2005, 12:32 pm
Ron, we have a twele year daughter, who has lot from your site. I feel proud when she visit your site. Each times she visit, she learns. Out Thanks for wonderful and nice for kids
Neeta Devi
Comment by Steve on Mar 10, 2005, 9:16 am
Hello Ron,

I've read several tutorials on displaying random content in PHP, but yours is by far the easiest to understand.

part 1
Comment by Steve on Mar 10, 2005, 9:18 am
Part 2...

I've searched and even asked the question in other forums with no one able to offer a solution so I'm going to pose the question to you. In your example, is there a way to make the selected array "stick" for a period of time? i.e. a new quote will be displayed after every page refresh.

« Previous [ 1 2 ] Next »
Post a comment
Sorry, you must be a registered member to post comments.

If you would like to register, you can do so here.
If you already have an account, please login.