Jump to content

[SOLVED] random banner rotator script


joshgarrod

Recommended Posts

That is just a base to get a random image from a folder of images and display it on the page. You will have to add the link, and the table.

 

 

 

<?php
//This will get an array of all the .gif images in a folder
$img_array = glob("/path/to/images/*.gif");
//Pick a random image from the array
$img = array_rand($img_array);
//Display the image on the page
echo '<table><tr><td><a href="http://site.com/their/link.html"><img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" /></a></td></tr></table>';
?>

<?php
//This will get an array of all the .gif images in a folder
$img_array = glob("/images/banners/home/*.gif");
//Pick a random image from the array
$img = array_rand($img_array);
//display image
echo '<td rowspan=7 valign=top><table><tr><td><a href=www.google.com><img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" /></a></td></tr></table></td></tr>';
?>

 

I tried the script and got

 

Warning: array_rand() [function.array-rand]: First argument has to be an array in /home/grewa/public_html/beltd.co.uk/includetop.php on line 98

 

line 98 is

$img = array_rand($img_array);

 

My whole script is

 

					<a href="gallery/index.php">
					<?php
					//This will get an array of all the .jpg images in a folder
					$img_array = glob("gallery/images/*.jpg");
					//Pick a random image from the array
					$img = array_rand($img_array);
					//Display the image on the page
					echo '<img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" width=100 />';
					?>
				</a>

 

 

Try this:

				<a href="gallery/index.php">
					<?php
					if(!file_exists('gallery/images')){echo 'Directory gallery/images Not Found';}
					//This will get an array of all the .jpg images in a folder
					$img_array = glob("gallery/images/*.jpg");
					//Pick a random image from the array
					$img = array_rand($img_array);
					//Display the image on the page
					echo '<img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" width=100 />';
					?>
				</a>

Provided the link is the same as the image you can do what I have done.

 

echo '<a href="'.$img_array[$img].'.php">';

echo '</a>';

 

If you put that either side of the image, then it will take you to the image with a .php at the end, for example for image logo.png you would be take to logo.png.php which you could set to redirect to where you wanted.

 

So example code would be

 

						<?php
					if(!file_exists('gallery/images')){echo 'Directory gallery/images Not Found';}
					//This will get an array of all the .jpg images in a folder
					$img_array = glob("gallery/images/*.jpg");
					//Pick a random image from the array
					$img = array_rand($img_array);
					//Display the image on the page
					echo '<a href="'.$img_array[$img].'.php">';
					echo '<img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" width=100 />';
					echo '</a>';
					?>

 

 

I wrote this one session based to never show the same banner twice

<?php
$header_img = $_SESSION['head_img'];
while($header_img === $_SESSION['head_img']){
	$header_img = rand(0,count(glob(ROOT."images/headers/*.jpg"))-1);
}
$_SESSION['head_img'] = $header_img;
?>

 

So you simply use the $header_img as the img source properly adjusted to the right folder

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.