轉載: ASP精品屋 錢豐云
首先你需要一個數據庫放置你的廣告,我們共用了2個表: blBanners 和 tblVendors表:
tblBanners結構表如下:
bID - auto number (廣告ID)
bBanner - text (圖像文件)
bUsedViews - number (# 標準的廣告顯示次數)
bTotalViews - number (# of impressions the vendor has paid for)
bClicks - number (# 標準的廣告點擊次數)
bURL - text (網站URL)
bShow - yes/no (用來標識廣告是顯示還是隱藏)
vID - number (賣主 ID)
tblVendors表結構如下:
vID - autonumber (賣主 ID - 鏈到tblBanners.vID)
vName - text (賣主的名字)
etc..........
第一步
現在,這個數據庫已經建立了,你需要有隨機地在我們的網頁中顯示廣告并且計算顯示次數。:
DSN鏈接不講了,如果不會,你可以到以下地址看看:
http://www.askasp.com/toolbox.asp?Expand=True&ID=2#tool
如果是SQL,可以采用下面的相似語句:
SQL = "SELECT tblBanners.bID, tblBanners.bImage, tblBanners.bUsedViews, tblBanners.bLastViewed "
SQL = SQL & "FROM tblBanners "
SQL = SQL & "WHERE (((tblBanners.bShow)=True) AND ((tblBanners.bTotalViews)>[tblBanners].[bUsedViews]));"
在上面的SQL語句中,只有當bShow標志是True而且UsedViews(顯示客戶廣告次數)小于TotalViews(客戶總顯示廣告次數)的
記錄才作選取(下面不翻譯了,深夜了:)。
Now that we have all of the banners that we can display, we need to display a random one. We can do this
by grabbing the total number of banners, moving to the first record, and the moving to a random number,
for example:
Dim rndMax, rndNumber
Randomize
rndMax = Int(RecordSet.RecordCount)
rndNumber = Int(RND * rndMax)
RecordSet.Move rndNumber
Now that we have moved to our random banner, we now need to display the banner on our page (I am sure you
know how to do that, so I wont bore you with the details). However, Instead of using the banner's URL in
the link, we are going to use a redirect page so we can count the clicks. All we need to do is use the
banner ID in the HREF tag, for example:
a href="redirect.asp?ID=<%= BANNER ID %>"
Now that we have the link set up, we can move on to our redirect.asp page. On this page, we are going to
grab the ID that we are passing in the Query String, and grabbing the RecordSet that matches. Once we have
the RecordSet, we can grab the banner's URL, increase the Clicks by 1, and send the user to the
destination URL. Below is the code for the redirect.asp page:
<%
If Request.QueryString("ID") = "" Then
Response.Redirect("default.asp")
End If
Dim varSiteToRedirect, varURLToRedirect
varSiteToRedirect = Int(Request.QueryString("ID"))
SQL = "SELECT tblBanners.bID, tblBanners.bURL, tblBanners.bClicks "
SQL = SQL & "FROM tblBanners "
SQL = SQL & "WHERE (((tblBanners.bID)=" & varSiteToRedirect & "));"
varDatabaseName = "ask_asp_data.mdb"
%>
<!--#include file="common/data_conn_open.asp"-->
<%
If Not RecordSet.BOF Then
RecordSet.MoveFirst
End If
varURLToRedirect = RecordSet.Fields("bURL")
RecordSet.Fields("bClicks") = (RecordSet.Fields("bClicks") + 1)
RecordSet.Update
%>
<!--#include file="common/data_conn_close.asp"-->
<% Response.Redirect(varURLToRedirect) %>