一个Banner管理类程序(转译)

来源:岁月联盟 编辑:zhuzhu 时间:2003-07-11
调用方式
1.  .Banner(Width, Height)
     .Banner方法是用来告诉Banner管理类程序你希望在客户端显示的是多大的Banner.
   I.  Width: <Banner宽度>
   II.  Height: <Banner高度>
2.  .Click(Banner_ID, Banner_URL)
     这个方法是程序员很少使用的.它是作用是用在客户端单击Banner统计
   I.  Banner_ID: <被单击Banner的编号>
   II. Banner_URL: <Banner链接的网址,例如:BannerMaster.asp?ID=1&URL=www.banneryouselected.com>

创建一个名为BannerMaster的数据库或<a href="http://asp-code.aspsamples.com/fpdb/BannerMaster.mdb">Download</a>
在新数据库中添加下面的列
" Banner_ID [1...2...3..]Int Don"t allow NULLs and Check Identity Seed 1
" Banner_Vendor [ex Monster.com] Varchar 50 Allow NULLs
" Banner_Width [ex 468] int AllowNULLs
" Banner_Height [ex 60] int AllowNULLs
" Banner_Alt [ex Monster Job Search] Varchar 50 AllowNULLs
" Banner_URL [ex www.monster.com] Varchar 50 AllowNULLs
" Banner_File [ex monster_logo-1.gif] Varchar 50 AllowNULLs
" Banner_Path [ex images/ads/] Varchar 50 AllowNULLs
" Banner_Imp_Purchased [ex 10000] Varchar 50 AllowNULLs Default 1000
" Banner_Imp_Current [ex 2343] Varchar 50 AllowNULLs Default 1
" Banner_Imp_Total [ex 120034] Varchar 50 AllowNULLs Default 1
" Banner_CT_Current [ex 23] Varchar 50 AllowNULLs Default 1
" Banner_CT_Total [ex 1200] Varchar 50 AllowNULLs Default 1
" Banner_CT_Percent [ex 1.01] Varchar 50 AllowNULLs Default 0
" Banner_Account_Active [ex True] Varchar 50 AllowNULLs Default True

将Banner添到数据库.
创建名为BannerMaster.asp 的文件,代码如下:

<!-- #include virtual="BannerMasterTestClass.asp" -->
<%
Dim Banner_URL
Dim Banner_ID
Banner_URL = Request("B")
Banner_ID = Request("ID")
   Call bm.Click(Banner_ID, Banner_URL)
   Response.Redirect "HTTP://" & Banner_URL
%>

创建名为BannerMasterTestClass.asp的包含文件,代码如下:
<%
"*******************************************************************************
"打开数据库
"*******************************************************************************
"定义连接数据变量
dim Server_IP
dim Database_Name
dim Table_Name
dim User_Name
dim Password

Server_IP = "xx.xx.xx.xx"
Database_Name = "BANNER_DATABASE"
Table_Name = "BANNER_MASTER"
User_Name = "Logon_Name"
Password = "User_Password"


Set DBConnection = Server.CreateObject("adodb.connection")
DSN = "DRIVER={Microsoft Access Driver (*.mdb)}; "
DSN = DSN & "DBQ=" & Server.Mappath("BannerMaster.mdb")
DSN = DSN & ";UID="&User_Name&";PWD="&Password
DBConnection.Open DSN


"*******************************************************************************
"创建类
"*******************************************************************************
Class BannerMasterClass
Public Error_Trapping_On
Public SQL_Debug
Public BannerArray "Avb outside of function
Public Number_Of_Banners

"*******************************************************************************

"*******************************************************************************
"类初始化
"*******************************************************************************
Private Sub Class_Initialize()
If SQL_Debug = True Then
Response.write "Defaults Set <br>"
End If
End Sub

"*******************************************************************************
" Banner Master
"*******************************************************************************
Public Function Banner(Banner_Width, Banner_Height)
If Error_Trapping_On <> False Then On Error Resume Next
If SQL_Debug = True then Response.Write "<br>FUNCTION Banner Called<br><br>"
If Banner_Width = "" OR Banner_Height = "" Then
If SQL_Debug = True then Response.Write "You must specify a Banner Width and Height <br>"
Banner = "Banner Width and Height<br>"
Exit Function
End If

"*******************************************************************************
"创建固定大小的活动Banners
"*******************************************************************************
Dim s
s = "SELECT Banner_ID FROM " & Table_Name & " WHERE "
s = s & "Banner_Width = "" & Banner_Width & """
s = s & " AND Banner_Height = "" & Banner_Height & """
"s = s & " AND Banner_Imp_Current < Banner_Imp_Purchased"
s = s & " AND Banner_Active = "True""
If SQL_Debug = True then Response.Write s & "<br>"
Set RS = DBConnection.Execute(s)
Select Case RS.eof
Case False
BannerArray = RS.getrows
Set RS = Nothing
Number_Of_Banners = Cdbl(UBound(BannerArray,2))+ 1
If SQL_Debug = True then Response.Write "Number of banners =" & Number_Of_Banners & "<p>"
Case true
If SQL_Debug = True then Response.Write " /- returned no Records<br><br>"
End Select
"--随机抽选Banner--
Randomize
SelectedBanner = (Int((Number_Of_Banners * Rnd) + 1))
If SQL_Debug = True then Response.Write "Random SelectedBanner=" & SelectedBanner & "<br><br>"
Banner_ID = BannerArray(0, SelectedBanner - 1)
If SQL_Debug = True then Response.Write "Random Banner_ID=" & Banner_ID & "<br><br>"
"--重新得到Banner数据--

s = "SELECT Banner_Alt, Banner_URL, Banner_File, Banner_Path, "
s = s & "Banner_Imp_Current, Banner_Imp_Total FROM " & Table_Name & " WHERE "
s = s & "Banner_ID = "" & Banner_ID & """
If SQL_Debug = True then Response.Write s & "<br>"
Set RS = DBConnection.Execute(s)
Select Case RS.eof
Case False

Banner_URL = RS("Banner_URL")
Banner_Alt = RS("Banner_Alt")
Banner_File = RS("Banner_File")
Banner_Path = RS("Banner_Path")

Banner_Imp_Current = RS("Banner_Imp_Current")
Banner_Imp_Total = RS("Banner_Imp_Total")
Set RS = Nothing
If SQL_Debug = True then Response.Write "Banner URL=" & Banner_URL & "<p>"

"创建链接字符串
a = "<a href=BannerMaster.asp?ID=" & Banner_ID & "&B=" & Banner_URL & ">"
a = a & "<img alt=""" & Banner_Alt & """ border=""0"" src="""& Banner_Path
a = a & Banner_File & """ width=""" & Banner_Width & """ height="""
a = a & Banner_Height & """></a>"


"返回链接字符串给类
Banner = a

"*************************************************
"*** 累计当前Banner单击次数和总数 *********
"*************************************************
s = "UPDATE " & Table_Name & " SET "
s = s & "Banner_Imp_Current = "" & (Banner_Imp_Current + 1) & "", "
s = s & "Banner_Imp_Total = "" & (Banner_Imp_Total + 1) & """
s = s & " WHERE Banner_ID = "" & Banner_ID & """
If SQL_Debug = True then Response.Write s & "<br>"
Set RS = DBConnection.Execute(s)
Case true
If SQL_Debug = True then Response.Write "/- returned no Records<br><br>"
End Select

End Function

"-- 单击事件处理函数 --

Public Function Click(Banner_ID, Banner_URL_Clicked) "累计Banner_CT_Current, Banner_CT_Total, Banner_CT_Percent and Banner_Imp_Current

s = "SELECT Banner_CT_Current, Banner_CT_Total, Banner_Imp_Current"
s = s & " FROM " & Table_Name & " WHERE "
s = s & "Banner_ID = "" & Banner_ID & """
If SQL_Debug = True then Response.Write s & "<br>"
Set RS = DBConnection.Execute(s)
Select Case RS.eof
Case False
"累加CT
Banner_CT_Current = RS("Banner_CT_Current")
Banner_CT_Total = RS("Banner_CT_Total")
Banner_Imp_Current = RS("Banner_Imp_Current")
Banner_CT_Current = Banner_CT_Current + 1
Banner_CT_Total = Banner_CT_Total + 1
Banner_CT_Percent = (Banner_CT_Current/Banner_Imp_Current )*100
Banner_CT_Percent = FormatNumber(Banner_CT_Percent, 2)

"写回CT到库
s = "UPDATE " & Table_Name & " SET "
s = s & "Banner_CT_Current = "" & Banner_CT_Current & "", "
s = s & "Banner_CT_Total = "" & Banner_CT_Total & "", "
s = s & "Banner_CT_Percent = "" & Banner_CT_Percent & """
s = s & " WHERE Banner_ID = "" & Banner_ID & """
If SQL_Debug = True then Response.Write s & "<br>"
Set RS = DBConnection.Execute(s)

"返回当前Banner单击次数
Clicks = Banner_CT_Current

Case true
If SQL_Debug = True then Response.Write "/- returned no Records<br><br>"
End Select

End Function
End Class

"*******************************************************************************
" 设置全局变量
"*******************************************************************************
"创建Banner管理对像BannerMaster
Dim BannerMaster
Set BannerMaster = New BannerMasterClass
BannerMaster.Error_Trapping_On = False
BannerMaster.SQL_Debug = False

%>