Jump to content

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>';
?>

 

$img_array = glob("/images/banners/home/*.gif");

 

That assumes the random images are in a folder named home, which is in a folder named banners which is in a folder named images.  You need to adjust that to match where your images are.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.