Random Text or Image Generation

Image and Text Rotation Tutorial contributed by Maddy.

Many people have these to randomize things in their site and if you belong to their group here is the perfect script for that.

Firstly, have all the requirements for PHP, then create 1 Text file and name it as quotes.txt and enter all the relevant information. If you want Text , just type in all the stuff you need. Take care that each proverb/quote/thought/idea/ad/imgsrc should be in a separate line. Well, i’m going for images and this is how my quotes.txt file looks ^^

  |<img src="http://..../someimage.jpg" border="0" alt="Link US">|
  |<img src="http://..../someimage.jpg" border="0" alt="Link US">|
  |<img src="http://..../someimage.jpg" border="0" alt="Link US">|
  |<img src="http://..../someimage.jpg" border="0" alt="Link US">|
  |<img src="http://..../someimage.jpg" border="0" alt="Link US">|
  |<img src="http://..../someimage.jpg" border="0" alt="Link US">|

Now to make it work, Put this code in your header file. I’ll explain what they mean in a minute, but first the important instructions. Please place the quotes.txt in the same directory as header.txt , or you should enter the entire path like this ‘c:\domains\yourname\..\quotes.txt’ . I hope you understood ^_^

  <?php 

  $quotes_array=file('quotes.txt'); 

  $quote=$quotes_array[rand(0,count($quotes_array)-1)];

  echo $quote;

  ?>

The first command takes all the contents of the file and makes it into an array.

$quote does the randomizing. The function ‘count’ counts the number of lines in the array and lessens it by 1 and gives the arguments to the rand function.The function ‘rand’ generates a random value. For example: If you want a random number between 5 and 15 , u can use rand(5,15), now ours could be like rand(0,5)

Finally, we echo out the line. It’s as simple as that.

If it’s text, it gets displayed out as you want, you could have echo "<b><u>$quote</u></b>"; , if it’s an image the above code will do. Hope it was of some help. Thank you!!!

November 6th, 2004 |
Posted in PHP, Web Design

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Random Text or Image Generation”


Hi,

I’m not very familiar with PHP. I tried out your Random Text or Image Generation and it worked the first time! Thanks for sharing the code and explanation.

Tiki

by Tiki Tiki — December 27, 2006 @ 11:04 am

Leave a Reply