Rotating images on an HTML page ...

OTrap

Elite Member
Joined
Jul 12, 2008
Messages
2,314
Reaction score
1,064
Suppose I have a .txt file of a thousand image URLs (we'll call the file image.txt), and I have a place on my site where I want those images displayed, but I want the image that displays to rotate through the list each time the page loads, so that each time the page loads, the next image down in the list displays in that spot.

Is this possible, and if so, how would I go about that?
 
OK. A couple questions so I know exactly what you're wanting to do.

Does it need to display just a single image for each page load, or use that image as a start position for a slide show on the page?

Does it have to display the images in order for each page load, or can it be just a random image each time?
 
Here's the code to display a totally random pic from the list (untested but it should work):

PHP:
<?php

// Path to your file with your pic urls in it
// One url per line
$filename = 'images.txt';

// Load list of filenames
$lines = file($filename);
foreach($lines as $line) {
	$pics = trim($line);
}
shuffle($pics);

// Pick a random image
$pic = array_shift($pics);

// Display pic
echo "<img src=\"$pic\">\n";

?>

If you want to show the pics in order then you have to set a cookie each session to keep track of what pic you're up to. Don't have time to write it out right now, but if you search my posts for "cookie" I posted something similar a while back.
 
Try this... It sets a session cookie to keep track of which pic you're up to, and loops through the pics from start to end, then starts at the beginning again.

Code:
<?php

// Path to your file with your pic urls in it
// One url per line
$filename = 'images.txt';

// Load list of filenames
$lines = file($filename);
foreach($lines as $line) {
	$line = trim($line);
	if(!empty($line)) {
		$pics[] = $line;
	}
}

// Max pic count
$max = count($pics) - 1;

// Set / increment the cookie value
if(isset($_COOKIE['views'])) {
	// Reset views if we reached end of $pics
	// otherwise increment views by 1
	if($_COOKIE['views'] >= $max) {
    	setcookie('views', 0);
	} else {
		$views = $_COOKIE['views'];
		$views++;
    	setcookie('views', $views);
	}
} else {
    setcookie('views', 0);
}

// Which pic to display
$p   = $_COOKIE['views'];
$pic = $pics[$p];


?>
<html>
<head>
<title>Blah blah</title>
</head>
<body>
<?php
     echo "Cookie value: ". $_COOKIE['views'] ."<br>";
    // Show the fuckin pic
    echo "<img src=\"$pic\"><br>\n";
?>
Blah blah blah
</body>
</html>

Edit: just tweaked the script.
 
Last edited:
here's some code for rotating images if you're not working with php.

I use it to rotate clickable banner ads but could be tweaked for what you need I guess.

Code:
<SCRIPT language=JavaScript>
 var img_width = "468";
var img_height = "60";
var img_title = "WHATEVER YOU WANT";
  var ad=new Array()
//insert here your images src
ad[0]='http://www.yoursite.com/images/image1.whatever';
ad[1]='http://www.yoursite.com/images/image2.whatever';
ad[2]='http://www.yoursite.com/images/image3.whatever';
ad[3]='http://www.yoursite.com/images/image4.whatever';
ad[4]=''http://www.yoursite.com/images/image5.whatever';
ad[5]=''http://www.yoursite.com/images/image6.whatever';
ad[6]='http://www.yoursite.com/images/image7.whatever';
 var links=new Array()
//insert here your links
links[0]='http://www.linksite1.com';
links[1]='http://www.linksite2.com';
links[2]='http://www.linksite3.com';
links[3]='http://www.linksite4.com';
links[4]='http://www.linksite5.com';
links[5]='http://www.linksite6.com';
links[6]='http://www.linksite7.com';
 var xy=Math.floor(Math.random()*ad.length);
 var xy=Math.floor(Math.random()*ad.length);
document.write('<a href="'+links[xy]+'" target="_blank"><img src="'+ad[xy]+'" width="'+img_width+'" height="'+img_height+'" alt="'+img_title+'" border="0"></a>');
</SCRIPT>

(WOW - I can't beleive I answered a coding question!!!)
 
PERFECT, Autumn! JUST what I was looking for! Thanks and reps to all of you (unless I repped Greywolf too recently)!

Grey, how's the plan coming?
 
Does anyone have a php based rotating ad script that has an admin area so you can track the amount of impressions and clicks on an ad?
 
Back
Top