-Log in
-Register (free)

-RSS Feeds
-New MyDesk Module
-Update to Profile

Alternate color for RS looping with For Next
Northwind / ASP - Nested select
ASP SQL Injection

 Lesson 2: Banner rotator
Lesson 2: Banner rotator

Author: Alexander Haneng
Difficulty: Easy
Requires: ASP
Demo: demo
Download: Lesson2.zip
Learn to make a banner rotate include file. All code is available. View the demo to see the result.

When I built the first version of Haneng.com I needed a banner rotator, but I felt that the included "Ad Rotator Component" didn't cut the mustard. I considered using a SQL database for the job, but to relieve the stress on it I decided to use pure VBScript in an include file. For this lesson I have removed a lot of the fancy stuff like the counters (keeping track of the number of displayed banners, clicks per banners etc.), but this is easy to add. This result in an easy script, that even runs on PWS.

Finding a random value:
We need to make a random value, so that we can display random banners. We use the built in random function to get a random number that we use to select one of the banner. To get this value I use this code:

LowestNumber = 1
HighestNumber = 10
RandomNumber = INT((HighestNumber-LowestNumber+1)*Rnd+LowestNumber)

The RANDOMIZE must come first, it ensures that the value we get really is random. (We don't want our script to only show one banner all the time). We set the LowestNumber to the lowest value we want and the HighestNumber to the highest value we want. So when we set the lowest number to 1 and the highest number to 10 the RandomValue variable will get one of the following values: 1, 2, 3, 4, 5, 6, 7, 8, 9 or 10.

Using the last digit to select a banner:
OK, now we got a random number. Now I use the SELECT CASE function to select a banner. The code will look like this:

LowestNumber = 1
HighestNumber = 10
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>
<%CASE "6"%>
<A HREF="link6.htm"><IMG SRC="banner6.gif" BORDER=0></A>
<%CASE "7"%>
<A HREF="link7.htm"><IMG SRC="banner7.gif" BORDER=0></A>
<%CASE "8"%>
<A HREF="link8.htm"><IMG SRC="banner8.gif" BORDER=0></A>
<%CASE "9"%>
<A HREF="link9.htm"><IMG SRC="banner9.gif" BORDER=0></A>
<%CASE "10"%>
<A HREF="link10.htm"><IMG SRC="banner10.gif" BORDER=0></A>

Just put the URL's where the "link" is, and make some banners that you link to in the IMG tag. And boom you got your own banner ad rotator!

Save the above code in a text file call it banner.asp, modify the links and make ten banner images. You can also download the Lesson2.zip file, unzip it and open banner.asp in a text editor (like Notepad) to modify it. (It also contains 10 sample banner images).
Now use this line of code on all the pages where you want the banners to appear:

<!--#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. Just make sure that banner.asp is in the same directory as the script you include it in. 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 in stead:

<!--#INCLUDE VIRTUAL="banner.asp"-->

You can display much more than random banners with this code. You can make "Tip of the day", "News flash" or a "Random link". For banner use, you should add counters. Use the "Counters component", a text file or a SQL database to track how many times a banner has been shown, how many times it has been clicked etc.

A last comment:
I hope you can use this code, it's pretty basic, but everybody have to start somewhere right?

Where to go next:
Take a look at the other lessons.

1: Intro to ASP
3: Get a live chat on your page!
| Info |
© Copyright 1997-2016 Alexander Haneng