A complete banner advertising system


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.

You might also like...



Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Weeks of coding can save you hours of planning.”