programmingsite : code , tutorials and scripts
Navigation

home


ASP
ASP.net
C
C++
C#
PHP
Java
JavaScript
Powershell
VB.net

Visual Basic


Resources
code
tutorials
training

Ads
Products

 

 

Random Image Script




This is a simple random image script which is ideal for displaying basic images from a text file . In future examples we will expand on this and build some simple banner rotation systems.

The first step that you need to do is to create your file for storing your images and then insert the names of the images.

In the example here I called the file images1.txt .

Each one of the entries is on a seperate line and in this case because I stored the files in a sub-directory I inserted that also . the structure of the file was like this

image/banner1.gif
image/banner2.gif
image/banner3.gif

and so on. Now we get to the script that will display a random image and again this is straight forward enough.

<?php
#random images example
#this is your file
$file = "images1.txt";
#open the file
$fp = file($file);
#generate a random number
srand((double)microtime()*1000000);
#get one of the entries in the file
$random_image = $fp[array_rand($fp)];
#display the entry
echo "<img src='$random_image'></img>";
?>

Nothing ground breaking here , we open a file , we then generate a random number, we then get a random entry from the file and store this in the variable $random_image and then we output this as some HTML.

Note that in this example we have saved this as a seperate file and included it on the page . If the file was called random.php then we put the following code where we want the image to appear

<?php include("random.php"); ?>

Here is an example below








Products
500 Java Tips E-book
PHP editor
PERL editor
Beginning Java
Beginning Visual Basic
Learn VB.net
Learn VB 6
VB and databases
ASP image library
C++ builder programming
C++ fundamentals
Asp Studio 2005
Xtra PHP
Online PHP Script Encoder
Comparison

Links
Make money selling software
Uk shopping
UK price comparison
US price comparison
free articles
free stuff
Gambling resources
Software directory
XBox cheats and more
Sexy free wallpapers
Hosting resources
Nintendo Wii info
Beginners PHP
Health and fitness
Domain names
Dating site
Nintendo DS resources
Dropshippers
Maxi directory
Perfume and fragrances
PC shopping, tips and info
Magazine superstore

Ads