Quantcast
Channel: Web Services Team » PHP
Viewing all articles
Browse latest Browse all 2

Another PHP image rotator w/ captions

0
0

I was tasked with creating an image rotator for a page where the image would change every time the page loaded. The hook was to also get a text caption area to change along with it. So, without the use of flash (because the image and caption are located in two different areas of the page), here is a little php to make that happen.

Create a php script called whateveruwant.php


<?

// List the images that you want to rotate with their captions into variables.
$image1='1.jpg'; $imagecap1='This is the caption for image 1.';
$image2='2.jpg'; $imagecap2='This is the caption for image 2.';
$image3='3.jpg'; $imagecap3='This is the caption for image 3.';
$image4='4.jpg'; $imagecap4='This is the caption for image 4.';
$image5='5.jpg'; $imagecap5='This is the caption for image 5.';

// Create an array of the variables above.
$array_set = array(
$image1=>$imagecap1,
$image2=>$imagecap2,
$image3=>$imagecap3,
$image4=>$imagecap4,
$image5=>$imagecap5
);

// This creates two new separate arrays: for the images, for the captions.
// Then randomly selects one key=>value set and returns the image/caption combo into variables.

$img_array = array();
$cap_array = array();
$i=1;
foreach($array_set as $key=>$val){
$img_array[$i]=$key;
$cap_array[$i]=$val;
$i++;
}
$rand_keys = array_rand($array_set,1);

$A_image=$img_array[$rand_keys[0]];
$A_cap=$cap_array[$rand_keys[0]];

?>

Then, you include the whateveruwant.php script at the top of your page. Then you put the image and the caption wherever you want on the page (remembering to put the appropriate location to your images in the source attribute)


<? include('whateveruwant.php'); ?>
<html>
<body>
</body>
<? echo '<img src="images/'.$A_image.'"/>'; ?>
<? echo '<p>'.$A_cap.'</p>'; ?>
</html>

The End.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images