Basic Random Image (perfect for header image rotation)
Published on: 2005-09-24 - Views: 15186
In this tutorial we'll see a basic script to display random images.
It's really simple but really useful too.
You can use it, for example, to display different header images, so each time that a visitor comes to your website or opens a new page he gets a different header.
You need to create a folder to store the images that you want to rotate (here I have named the folder "shotrotation").
All the images will have to be named "1.jpg", "2.jpg", "3.jpg" etc, images with different names won't show!
Now let's have a look at these simple lines of code.
<?php $images = 10; //this is the number of images we have to rotate, note that if //it\'s bigger than the number of images you will get inexistent images //selected, if it\'s smaller some images won't ever be displayed $path = "shotrotation/"; //this is the path to the folder where //the images are stored: you can use a local path as I did, or an //absolute path like http://www.yourdomain.com/shotrotation/ note that //local paths have to start with the folder name (don't put "/" in //fron of it) and terminate with "/" $random = rand(1,$images); //with the rand PHP function we generate //a random number from 1 to our max allowed value echo "<img src=$path"."$random".".jpg"." border=\'0\'>" //here we print //the html code that will show the image ?>


