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.

Random content can make your web pages feel a bit more alive, it can also give the browsing experience for your visitors a bit of a boost. Luckily, random content is pretty easy to accomplish using PHP.

For this tutorial I'll use random quotes as an example. First of all, we need to setup an array of our quotes.

<?php

$quotes 
= array(
'Impossible is a word to be found only in the nextline
dictionary of fools.<br />&mdash; Napoleon Bonaparte'
,

'A person who never made a mistake never tried any nextline
thing new.<br /> &mdash; Albert Einstein'
,

'Show me a thoroughly satisfied man and I will show nextline
you a failure.<br />&mdash; Thomas A. Edison'
,
);

?>

Now that we have our quotes, all we need to do is grab one out and echo it. If you remember from the Utilizing Arrays tutorial, all of our array items have keys, in this case, they are numerical keys. Meaning that if you were to do somthing like..

<?php
   
echo $quotes[1];
?>

You would end up with the value of the the second item in the array. Remember array keys always start at 0, so 1 is the second item, not 2. Having said that, we can use the rand() function to generate a random number, which we will use to grab a random key from the array. Here's an example:

<?php
   
// generate a random number between zero and two
   
$rand rand(02);

   
// output the random quote
   
echo $quotes[$rand];
?>

The code above will print one of the quotes in the $quotes array to the page. But wait, what happens if you add more quotes to the array? You'll need to edit the line that generates the random number, and increase the 2 by however many more quotes you added.

However, that can become annoying. So, instead, we're going to modify the rand() function call, and change the second parameter to automatically count the number of items in the array using count().

<?php
   
// generate a random number between zero
   // and the number of quotes -1
   
$rand rand(0, (count($quotes)-1));
?>

You might be wondering why I subtracted one from the total number of quotes. This is because, if you had 3 quotes in an array, the highest key in that array is 2. So, in order to keep our random number from going over the number of our highest key, we just subtract one from the second parameter of the rand() function.

Now, if you put it all together, you come up with this:

<?php
$quotes 
= array(
'Impossible is a word to be found only in the nextline
dictionary of fools.<br />&mdash; Napoleon Bonaparte'
,

'A person who never made a mistake never tried any nextline
thing new.<br /> &mdash; Albert Einstein'
,

'Show me a thoroughly satisfied man and I will show nextline
you a failure.<br />&mdash; Thomas A. Edison'
,
);

/*
generate a random number between zero
and the number of quotes minus one
*/
$rand rand(0, (count($quotes)-1)); 

// output the random quote
echo $quotes[$rand];
?>

That's it! Pretty simple, eh? Go ahead and test it out, play around with it. See what more you can add to it. When you're done, have a look at the second page, which will show how you can produce multiple random things, like I have done for my random affiliates box.

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