CoverYourASP --> Advertising --> Part 1: Loading the banners for the day

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
93 active users
9168 visitors today
8511 pages today
how is this done?
Tools I use

Copyright © 1999-2016 James Shaw.
All rights reserved.

RSS submissions

Now open source with SourceForge!

The advertising on my site is served entirely in-house, by code freely available for you to use. This article will step you through the code.

The banners are all stored in a database table, but I don't access the database to serve the ads. Instead, at the beginning of every day the site automatically loads the ads for that day into Application variables. How the "BrandNewDay" trigger works is discussed in a related article about tracking IP addresses.

Loading the days ads

// get current advertising banners
DBInitConnection  ( );

DBGetRecords ( 'SELECT BannerID,BannerDescr,BannerURL FROM Banners WHERE ClicksLeft > 0' );

Firstly, ads "qualify" to be shown that day if there are any purchased clickthroughs still available (for details on how I sell per clickthrough, not impression, read this). That's done with the SQL "WHERE ClicksLeft > 0".

var sBanners = new Array;
var nBannerIDs = new Array;
var nBanners = 0;

// loop through banners
while ( !oRecordSet.EOF )
   var nBannerID = oRecordSet ( 0 ) - 0;
   var sBannerDescr = '' + oRecordSet ( 1 );
   var sBannerURL = '' + oRecordSet ( 2 );

   // small image to track impressions first
   var sBanner = '<img src="BannerCounter.asp?ID=' + nBannerID + '" border=0 width=1 height=1>';

The resulting recordset is looped through, and banner ID, description and URL extracted. To track the ad impressions, a link to a small 1x1 "image" is created, with BannerCounter.asp as the target. More about that file later.

// add URL (patched through our counter)
sBanner += '<a href="BannerCounter.asp?ID=' + nBannerID + '&CYAredir=' + Server.URLEncode ( sBannerURL ) + '" target="CYAExternal">'

Next, the URL that will be called when the banner id clicked is assembled. The advertisers original URL, in the variable sBannerURL, is passed as a parameter into (again) BannerCounter.asp. Because I want to pass the URL into another file I have to first encode it. This replaces any special characters such as /?& into their hexadecimal equivalents.

// add image
sBanner += '<img src="images/ads/' + sBannerDescr + '.gif" border=0 width=468 height=60></a>';

The last of the HTML, the image of the actual banner, is added to the sBanner variable...

   // store banner IDs for later use
   nBannerIDs [ nBanners ] = nBannerID;

   sBanners [ nBanners++ ] = sBanner + '<p>';

   oRecordSet.moveNext ( );

...and the data stored into the temporary array. The loop continues with the next record in the recordset.

// now set data into Application variables
Application.Lock ( );

Application ( 'TotalBanners' ) = nBanners;

for ( var i=0; i<nBanners; i++)
   Application ( 'Banner' + i ) = sBanners [ i ];

Application.Unlock ( );

Outside the loop, after all the records have been processed and stored in the array, I finally move the data into Application variables, locking the Application first. The array was used just to make this lock/unlock section as short as possible.

Part 2: Serving the banners...

Featured sponsor
My favorite resources

Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!

CoverYourASP Mugs, T-shirts, caps - even Boxer shorts...
I don't make a penny from these, but they're a lot of fun! Don't you need a new mouse mat?

New Proposal Kit Professional 5.1
Brand yourself as a top professional: create quotes and amazing proposals and get many legal documents free!

The latter saved me 3 times the purchase price on the first day I owned it!

I share my content

Supporting ASPRSS

Do you need a quick and easy way to link to my articles? All the information you need is published with ASPRSS...