We first need to create a random number to use this script between the number of images that we are going to use, so yes you have guessed it , if you are going to use 5 images then obviously we need to create a random number between 1 and 5.
To create a number between 0 and 1 we can use the Rnd
function. So to create a number between 1 and 5 we must use the
line of code below in red. If you have 10 images then you would
just change the value of the HighestNumber to 10.
Note that to use the Rnd function properly you must put Randomize
somewhere before it. I should also point out that as the number
generated is decimal the Int function will chop off the decimal (5.122
would become 5).
Have a look at the following code.
<%
Dim LowestNumber, HighestNumber, RandomNumber
RANDOMIZE
LowestNumber = 1
HighestNumber = 5
RandomNumber = Int((HighestNumber-LowestNumber+1)*Rnd+LowestNumber)
%>
So having written the code that will get us a random number I am going to use the SELECT CASE function to select a banner or image. I have also put a hyperlink behind the banner/image.
<%
Dim LowestNumber, HighestNumber, RandomNumber
RANDOMIZE
LowestNumber = 1
HighestNumber = 5
RandomNumber = Int((HighestNumber-LowestNumber+1)*Rnd+LowestNumber)
SELECT Case RandomNumber
%>
<% Case 1 %>
<a href="link1.htm"><img src="banner1.gif"
border=0></a>
<% Case 2 %>
<a href="link2.htm"><img src="banner2.gif"
border=0></a>
<% Case 3 %>
<a href="link3.htm"><img src="banner3.gif"
border=0></a>
<% Case 4%>
<a href="link4.htm"><img src="banner4.gif"
border=0></a>
<% Case 5 %>
<a href="link5.htm"><img src="banner5.gif"
border=0></a>
<% End Select %>
You can change the links and substitute your own banner/images
names.
Save the above code in a fill called banner.asp, modify the links
and make 5 images/banners.
Now use this line of code on all the pages where you want the
banners to appear. Just make sure that banner.asp is in the same
directory as the script you include it in.
<!--#include file="banner.asp"-->
Using an include file makes the whole thing easier to maintain.
Just modify the banner.asp file, and all the pages on your site
are updated.
If you want to include banner.asp into scripts that are located
in subdirectories you can place banner.asp in the root of your
site and use this include statement instead.
<!--#include virtual="/banner.asp"-->
In Part 2 of Generate a Random picture with ASP we show you a real example that this site uses.
3 MONTHS FREE – ASP.NET Hosting w/ US and UK Data Centers – Click Here!
About | Contact | Advertise | Feedback | Hire Us | Link
Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2001-2009. All rights reserved.
Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.