• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1542
  • Last Modified:

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.
  • 2
  • 2
1 Solution
Julian HansenCommented:

in the html file


// 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.
Cornelia YoderArtistCommented:
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.
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

  <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();
<div id="images">
	<img src="test1.jpg" />
	<img src="test2.jpg" />
	<img src="test3.jpg" />
	<img src="test4.jpg" />
	<img src="test5.jpg" />

Open in new window

yyming10Author Commented:
Works perfect.  Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now