PDA

View Full Version : Random Display for a set of image, text and link URL


saouthpaw68
04-21-2007, 10:07 AM
Hi, I am a webmaster with nearly no php programming background. Please someone kindly help me with the code that enables me to randomly display image, alt text and herf link in 2 minutes interval. I have a set of 10 combinations. ie.
1: source image= "/image/01.jpg"
alt = "First Image"
herf = "/page/01.php"
2: source image= "/image/02.jpg"
alt = "Second Image"
herf = "/page/02.php"

10: source image= "/image/10.jpg"
alt = "Tenth Image"
herf = "/page/10.php"

Thank you in advance for your kind help.
Regards,
JDN

bauhsoj
04-21-2007, 07:23 PM
In order to show these image links at intervals you will need to call the script itself at intervals through a page reload.

The rest is simple:
# Define an array of images, as many as you like.
$images = array('source' => '/image/1.jpg', 'alt' => 'First Image', 'href' => '/page/1.php',
'source' => '/image/2.jpg', 'alt' => 'Second Image', 'href' => '/page/2.php',
# ...etc, etc, etc...
'source' => '/image/10.jpg', 'alt' => 'Tenth Image', 'href' => '/page/10.php'
);

# Choose a random number that is within the index range of the images array.
$rand = mt_rand(0, 9);

# Print the link output.
print('<a href="'.$images[$rand]['href'].'"><img src="'.$images[$rand]['source'],'" alt="'.htmlspecialchars($images[$rand]['alt']).'" /></a>');