Webmaster Tutorials    +   PHP Coding    +   3D Studio Max    +   Acrobat    +   Adobe Photoshop    +   ASP Coding    +   Bryce    +   C++    +   Cinema 4D    +   Cold Fusion    +   CSS    +   Dreamweaver    +   Fireworks    +   Flash    +   Illustrator    +   Javascript    +   Maya    +   Paint Shop Pro    +   Ruby on Rails    +   Visual Basic    +   Website Development





Random quote

Views Today: 1, Total Views: 3793, Submitted by: magnus on 21-06-2006

Need Help? Visit our webmaster forum!

This tutorial will show you how to make random quotes appear. This can also be used for other things like random pictures, links and everything you can store in an array.

Quote

<?PHP

$quotes = array(
"This is my quote",
"To be or not to be",
"<b>You can</b> <u>use html</u>");

$count = count($quotes);

srand(time());
$random = (rand()%$count);

echo $quotes[$random];

?>


The first part creates an array named $quotes.

After that comes $count = count($quotes);. It counts how many entries there are in the $quotes-array and stores it in a variable named $count. We are using this var in the next step.

the srand(time());
$random = (rand()%$count);
part creates a random number and stores it in a variable named $random. You can see that I am using the $count-variable. In this example it is 3. That is because of the 3 entries in the array that $count = count($quotes); counted.

The next part prints out a random entry from the array we created at the top. $random here equals a random number between 0-2. 0, 1, 2. If you didn't want it random you could write something like echo $quotes['2'];. That would print the last line of the array. Yes the third line. The counting starts at 0. 0,1,2,3,4,5.

If you want to print a random picture just put
<img src='address_to_image.jpg' alt='my picture'> in the array.

Need Help? Visit our webmaster forum!

User Comments

If you want to comment on this tutorial you must first register or login.


Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/cinedict/public_html/tutorialdash/tutorialview.php on line 287
  , 23-06-2006

Very simple and to the point. Thanks


Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/cinedict/public_html/tutorialdash/tutorialview.php on line 287
  , 18-07-2006

I dont see what this line is for, or what it does:

srand(time());

Can someone explain..?

- CBG


Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/cinedict/public_html/tutorialdash/tutorialview.php on line 287
  , 19-07-2006

I dont see the significance of:

srand(time());

Can someone please explain it?

Thanks, CBG.

Post A Comment

Fill out the following form to comment on this tutorial.

Your Comments