An ASP-based banner advertisement Management system (III.)

Source: Internet
Author: User
Tags chr count
Advertising three, display ads

This part of the procedure mainly has the following four purposes: first, refer to the display level of each advertisement, randomly select the display of the advertisement; second, update the display number of the ads in the database; third, the output of the banner ads HTML code; Four, save the display and click History records in the database history table.

There are two scripts to implement these features:

Showbanner.asp: Determines the advertisement to be displayed for this call, updates the display count record, and generates the HTML code for the banner ad.
Redirect.asp: redirect page. The ad HTML code generated by showbanner.asp will invoke the script, which is recorded by clicking Historical data and redirecting to the advertiser-specified page.
The showbanner.asp in this download package is also an ad Display sample page that can display different ads each time the page is refreshed. Below we follow showbanner.asp's implementation process to introduce the key steps.

㈠ Calculate display rank sum

This system uses the advertisement choice algorithm is: first calculates the current all available advertisement the display level sum, then produces a random number according to this and the value, then according to this random number to determine this display advertisement. The following code calculates the sum of display levels for the available ads:

Ntotalweight = 0
strSQL = "Select SUM (Weight) as Sumweight from" + _
"Advertisement WHERE Status=1"
Rs. Open strSQL, CN
If not Rs. EOF and not Rs. BOF Then
Ntotalweight = Rs. Fields ("Sumweight")
' Ntotalweight is likely to be a null value
If IsNull (ntotalweight) Then
Ntotalweight = 1
End If
End If
After calculating the ntotalweight, we can generate a random number based on that value, as follows:

Randomize
Nrandomnumber = Int (Rnd * ntotalweight) + 1
The program reads the available ad records from the database (see the code below), accumulates the sum of the weight of the read records, and when the new sum exceeds the random number generated here, the current record is used as the advertisement for this display.

㈡ analysis of available advertising records

The next task is to execute a query that extracts all the available AD records. As mentioned earlier, when traversing these available ad records, the sum of the weight values of the recorded records is Nweightcount, and when Nweightcount is equal to or greater than the previously generated random number Nrandomnumber, the program considers that the advertisement for this secondary display is found. Specifically as follows:

strSQL = "SELECT * from advertisement WHERE Status=1"
Rs. Close
Rs. Open strSQL, CN
Bdone = False
' Sum of weight values of read records
Nweightcount = 0
 
' Clear variables
Strimageurl = ""
Stralttext = ""
Strlink = ""
Nimagewidth = 0
Nimageheight = 0
Nweight = 0
Nadid = 0
Nadvertiserid = 0
Nviewlimit = 0
nimpressions = 0

While not Rs. EOF and not Rs. BOF and not Bdone
' Assigning a database value to a variable
' This algorithm is not efficient, but simplifies the control structure
Strimageurl = Rs. Fields ("ImageURL")
Stralttext = Rs. Fields ("AltText")
Strlink = Rs. Fields ("Link")
Nimagewidth = Rs. Fields ("ImageWidth")
Nimageheight = Rs. Fields ("ImageHeight")
Nweight = Rs. Fields ("Weight")
Nadid = Rs. Fields ("Adid")
Nadvertiserid = Rs. Fields ("Advertiserid")
Nviewlimit = Rs. Fields ("Viewlimit")
Nimpressions = Rs. Fields ("Impressions")
Nweightcount = Nweightcount + nweight
Rs. MoveNext
' Nweightcount is equal to or greater than the value of the random variable
If Nweightcount >= Nrandomnumber or Rs. EOF Then
bdone= true
End If
Wend
㈢ update display number of current ad

After you have determined the advertisement for this secondary display, the program can update the Impressions field of the ad record. If the new impressions value exceeds the display count limit (nviewlimit), you must also set the Status field to 0 (that is, this advertisement cannot be displayed again). The implementation code is as follows:

Nstatus = 1
Nimpressions = nimpressions + 1
If nimpressions >= nviewlimit Then
Nstatus = 0
End If
strSQL = "UPDATE advertisement Set status=" + _
CSTR (Nstatus) + ", impressions=" + _
CSTR (nimpressions) + "WHERE adid=" + _
CSTR (Nadid)
Rs. Close
Rs. Open strSQL, CN
㈣ Generate HTML code

After you have completed the preparation, you can then output the HTML code that displays the advertisement. The output of the HTML code can be divided into the following sections:

redirect page--> destination URL--> Other parameters--> picture marker
Why not link directly to the advertiser-specified URL? This is why, although this approach is simpler, we want to be able to record the number of clicks on the ad, and in the redirect page we can update the Clickthroughs field value of the ad.

In the HTML code that displays the advertisement, the redirected page is assumed to be redirect.asp, the target URL is from the database, the other parameters are mainly the advertisement number, The advertiser number, etc., redirect.asp use these parameters to record the Click history Record. Specifically implemented as follows:

Strhtmlcode = _
"<a href=redirect.asp?" + _
"link=" + Strlink + _
"&advertisementid=" + CStr (Nadid) + _
"&advertiserid=" + CStr (Nadvertiserid) + Chr (34) + _
"target=newframe><" + Chr (+) + CHR (10) + _
"></A>" + Chr + Chr (10)
The strhtmlcode here is the HTML code that displays the advertisement.

㈤ Record Other information

In order to provide advertising customers



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.