Boontech Media
BoonTech Media
Boontech Media

Random Image Generator

Using a random image generator on your website has many practical purposes, some of which include creating a rotating banner system or rotate advertising ads. Its a valuable piece of code on your website and this tutorial will show you a simple way to use it.
Code
<?php
$images 
5
$path "images/"
$random rand(1,$images); 
echo 
"<img src=$path"."$random".".jpg".">" 
?> 

Code
$images 5

This is where we define the number of images that will be displayed. If the number is larger than the number of images in your folder you will get nothing back on certain refreshes. If you put the number smaller than the number of images in your folder, you will only display images up to that number. The images must be named in numerical sequence so if you put the number at 4 and have 5 images, the #5 image will not be displayed.

Code
$path "images/";  

This is where you define the path to your images folder

Code
$random rand(1,$images);  

Here we use teh rand() function to create a random number starting from 1 and ending at the number defined in our "images" variable


Code
echo "<img src=$path"."$random".".jpg".">" 

Here we piece everything together and echo out the image. Note if you have any other extension than .jpg you will have to change it in this line.


The script in action
Boontech Media
Portfolio update
I apologize for the lack of updates over the past months. This past year has been incredibly hectic and I just haven't had time to add any new content to the site.
Posted Jun 18 2007 by Dan
RSS Feed
I just added an RSS feed for all my tutorials. Ill update it as I add tutorials so it will always be up to date and remind you about new tutorials released.
Posted Aug 26 2006 by Dan
New tutorials
I have just posted two new tutorials. Both on the same subject, browser dependent stylesheets but using different methods.
Posted Aug 25 2006 by Dan
Browser issues
Its come to my attention that there are a few more browser bugs in Opera, Safari and Netscape.
Posted Aug 19 2006 by Dan

 

Previous 1 2 3 Next

Hosting Login:





Register