Random Image Rotator on Page Refresh/Load

Hi Experts.

I am looking for a php or jquery script that will allow a web page to display a random image each time the page is refreshed or loaded.  The site is currently on a cms built on php and I can create a list of images output to a div or area on the page, but would like to hide them and display one at a time on refresh or page load.  Any help would be greatly appreciated.
yyming10Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:

in the html file

<?php

// assume that $images is an array with image names in

$random_image = $images[random(0, count($images) 1)];


<img src="<?php echo $random_image;?>" />

Adjust for paths and naming conventions.
0
Cornelia YoderArtistCommented:
0
yyming10Author Commented:
I appreciate the input, but the code already displays a list of the images within the image tag.  I would need something that would hide them all and then display randomly on refresh or page load.  Thinking it would probably need to be jquery/javascript.
0
Julian HansenCommented:
Ok but if you are going to modify the code to add the javascript then is it not possible to modify to only display the image you want?

Having said that here is some jquery code to hide the images


<html>
<head>
  <script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript">
$(function() {
	var els = $('#images img').get();
	var img_on=Math.floor(Math.random()*els.length)
	$('#images img').hide();
	$('#images img:eq(' + img_on + ')').show();
});
</script> 
</head>
<body>
<div id="images">
	<img src="test1.jpg" />
	<img src="test2.jpg" />
	<img src="test3.jpg" />
	<img src="test4.jpg" />
	<img src="test5.jpg" />
</div>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yyming10Author Commented:
Works perfect.  Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.