Click to See Complete Forum and Search --> : Banner/Button rotation
mjwest
09-14-2005, 07:24 AM
I have a site that has 30+- banners. I need to set them up so that all of them rotate positions upon loading. It doesn't matter what poisition they are in, they just need to not stay in the same position all the time. Any suggestions?
zingmatter
09-14-2005, 10:04 AM
I would use a server-side scripting language to place the images randomly. Using ASP:
<%
'// example using 3 images: image0.jpg image1.jpg image2.jpg
Dim images(3)
Dim n
Dim notChosen
Dim x
CONST MAX_ELEMENTS = 3
notChosen = True
x = 0
Randomize
n = int(2 * Rnd())
do while notChosen
if n > (MAX_ELEMENTS - 1) then
n = 0
else
images(n) = "image" & x & ".jpg"
x = x + 1
n = n + 1
end if
if x >= MAX_ELEMENTS then
notChosen = False
end if
loop
%>
<img src="<%= images(0) %>">
<img src="<%= images(1) %>">
<img src="<%= images(2) %>">
Prehaps not the simplest method but it works.
There's probably a client-side javascript you can use but that'll slow your load time and won't work if the user has the javascript turned off
Hope this helps
mjwest
09-14-2005, 10:28 AM
Thanks for the info. I know this is dumb, but I don't have any experience with asp. My experience is with html and limited at that. Is there a handy reference so that I can find out how to put an asp page together?
Would I have to redo my entire site in an asp format to do this?
True, if it's javascript they would have to have it turned on, but I'd just have to make that notation on the site. A sacrafice that would have to be made.
You assistance is appreciated.
MJWest
zingmatter
09-14-2005, 11:33 AM
If you want to use ASP then firstly you're host will need to support ASP scripting. It may support PHP (if it does I could rewrite the code in PHP) but you'd need to check. If ASP is supported then the file name would have to have named with .asp rather than .html at then end (or .php for PHP files).
To do the same in javascript you could do something like this:
<img src="" name="image0">
<img src="" name="image1">
<img src="" name="image2">
<script type="text/javascript">
<!--
setImages();
function setImages() {
var images = new Array(3);
var n = 0;
var x = 0;
var notChosen = true;
var MAX_ELEMENTS = 3;
n = Math.round(2 * Math.random());
while (notChosen) {
if (n > (MAX_ELEMENTS - 1)) {
n = 0;
} else {
images[n] = "image" + x + ".jpg";
x++;
n++;
}
if (x >= MAX_ELEMENTS) {
notChosen = false;
}
//now set the images
document.image0.src = images[0];
document.image1.src = images[1];
document.image2.src = images[2];
}
}
//-->
</script>
the tree
09-14-2005, 12:00 PM
If your server doesn't support ASP (fairly likely) and you don't want to use javascript (sensible) then you could use a PHP script.
Pick a file from a directory<?php
$directory = 'images/';
$images = scandir($directory);
$chosen = rand(1,count($images)) - 1;
echo '<img src="" alt="'.$directory.$images[$chosen].'">';
?>
zingmatter
09-14-2005, 12:27 PM
Neat code! Host having php isn't particularly likely to be available either, although granted, more likely than ASP. Why did you put the image path in the alt attribute?
mjwest
09-14-2005, 12:46 PM
Oh, man. I've got a learning curve to deal with here. I'm not in a position to re-write anything, so I'm got to ATTEMPT the javascript method. Even then it will be trial and error (mainly error). Wish me well. Thanks for your responses and I'll take and and all suggestions.