This function is responsible for the retrieval and display of a banner at random from our database. Firstly, we declare a new database connection and recordset. After this, we set the virtual path for the location of our banners folder in the strBanner_VirtualPath variable. Lastly, we open a connection to our database and set the ActiveConnection of our recordset object to point to this connection.
objRS.Open "select count(*) from banners"
intNumBanners = objRS.Fields(0).Value
if intNumBanners = 0 then
intRandom = int(rnd(1)*intNumBanners)
This next part of our code is where the random number generation takes place. Firstly, we use our recordset object to get a count of the number of records in our banners table. The query we have used (select count(*) from banners) returns a table with one field containing one value. If we executed this query using query analyser when we had three banners in our table, we would achieve this result:
After we store the return value into the variable intNumBanners, we run a quick check to make sure that there is at least one banner in our table. If there isn't, we exit the function and return nothing.
Now comes the random banner generation. First, we call the Randomize function which basically tells our ASP engine this: "When we call the Rnd() function, make sure we get a random number every time". It sounds confusing, but really, it isn't. Then we use the Rnd() function to generate a random integer between 1 and the number of records in our database (in my case, 3), followed by closing our recordset object.
Because we closed our recordset object previously, we must open it again. However, this time we select all the rows in the banners table as our query. Because we're using a recordset with a dynamic cursor (objRS.CursorType = adUseDynamic), we have the ability to move to any row in that recordset. objRS.Move intRandom uses our randomly generated number as the row to move to. So If we generated a random number of 3, we would move to the third row in our recordset (remember that recordsets are like a zero-based array, so we'd be moving to row number three but the row would have an index of 2).
The last line is used to build the HTML string that we will output in our browser. The code uses simple anchor and image tags to display our banner with a link. Notice the URL in the anchor tag (bannerclick.asp?bannerId=" & objRS("bannerId")). This is the next and final page that we will discuss. It's the page that handles the addition of click-thrus and re-directs the user to the URL that corresponds to that particular banner. More on that later, though.
objConn.Execute "UPDATE banners SET bannerImpressions =
bannerImpressions + 1 WHERE bannerId = " & objRS("bannerId")
set objConn = nothing
set objRS = nothing
The final part of our GetBanner function uses our connection object to increase the impression count for the randomly selected banner, and frees up the memory used by our connection and recordset objects.
I understand the function, now what?
So, now that we've stored our GetBanner() function in bannercode.asp, how do we display the banners on our Website? It's a simple matter of adding two lines of code to any existing ASP page. Lets look at an example:
<!-- #INCLUDE file="bannercode.asp" -->
<title> My Test Banner </title>
As you can see from the example above, we simply use an SSI (Server-Side include) to reference the file that holds our GetBanner function. Then we simply call this function with either the <%= GetBanner() %> method or <% Response.Write GetBanner() %> method.
In the next and final part of the tutorial, we'll create the bannerclick.asp page that handles the click-thrus and re-directions when a banner is clicked from our site.