I’ve been trying to figure out how to use the mysql support built into MT to display a random image in a banner, in an attempt to create dynamic content instead of plain old boring, static content. I’ve been able to load images into a database (I think), but when I try to echo them to a page it looks like gobbledegook, must be something weird with my encoding. So I found a php script instead which seems to more-or-less work, but the randomness gets stuck sometimes and images get repeated.
The php looks like this:
– rotate image #0 – use ‘i=1’
// for second, etc
// (c) 2004 David Pankhurst – use freely, but please leave in my credit
$images=array( // list of files to rotate – add as needed
“img1.gif”,
“img2.gif”,
“img3.gif”,
“img4.gif”,
“img5.gif” );
$total=count($images);
$secondsFixed=10; // seconds to keep list the same
$seedValue=(int)(time()/$secondsFixed);
srand($seedValue);
for ($i=0;$i<$total;++$i) // shuffle list 'randomly'
{
$r=rand(0,$total-1);
$temp =$images[$i];
$images[$i]=$images[$r];
$images[$r]=$temp;
}
$index=(int)($_GET['i']); // image index passed in
$i=$index%$total; // make sure index always in bounds
$file=$images[$i];
header("Location: $file"); // and pass file reference back
?>
The disadvantage of this method is that you have to manually enter the image details in the list above, which is a pain if you have hundreds of images.
It’d be much better to incorporate a mysql query. But I don’t know how to do that yet.