Welcome Guest Login or Signup LIVE CHAT | INSTANT MESSENGER | BOOKMARK US
logo
CATEGORIES:      
 

Random Images with php

By: amperspective
Mood: Other
Date: Apr 25, 2008
Music: None


Every now and then you want to randomize images, usually headers of sorts. This is pretty simple, with just 9 lines of PHP code you can rotate randomly between two images.

The PHP Script

Put this where you want to rotate your headers:

PHP:
  1. <?php
  2.     $images = array(
  3.         0 => 'image1.gif',
  4.         1 => 'image2.gif',
  5.     );
  6.     $image = $images[ rand(0,(count($images)-1)) ];
  7.     $output = "<img src="/mysite/randomimages/".$image."" alt="" border="0" />";
  8.     print($output);
  9. ?>

This will rotate between image1.gif and image2.gif, which are located in /mysite/randomimages/ on your server.

Adding more images is easy, just continue to add lines, starting with the next digit and has a corresponding file name, in the $images = array(); segment. This is the same code with five images instead of two:

PHP:
  1. <?php
  2.     $images = array(
  3.         0 => 'image1.gif',
  4.         1 => 'image2.gif',
  5.         3 => 'hello.gif',
  6.         4 => 'monkeys.gif',
  7.         5 => 'ninjas.gif',
  8.     );
  9.     $image = $images[ rand(0,(count($images)-1)) ];
  10.     $output = "<img src="/mysite/randomimages/".$image."" alt="" border="0" />";
  11.     print($output);
  12. ?>

Easy, isn't it?

Now rotate those headers, photos, or whatnot!

 

 

Article source (http://www.devlounge.net/articles/random-images-with-php) 














TERMS & CONDITIONS | HELP GUIDE | CONTACT US | INVITE | COMPATIBILITY | ABOUT US | GET INVOLVED | RSS FEEDS | ARCHIVE

mysiteprofile.com