Small site advertisement management using PHP

Source: Internet
Author: User
Tags random seed

Image link advertisements are probably the most commonly used online advertising models, but dynamic and random display of multiple advertisements is not that simple. The file upload function of PHP provides a solution for randomly displaying advertisements based on the weight value in the 468x60 pixel area (standard size of webpage advertisements), thus avoiding the development of FTP Program Or plug-in extensions are required in ASP and Perl Cai to implement this function.

In this example, It is debugged in PhP4 + MySQL + Apache for Win9x/2000, which consists of two parts: the addnewad program used to add new advertisements. php3 and showad. php3 is stored in the "test/guanggao/" directory under the PHP file directory of the server. Create a sub-directory "adbanner/" to store ad link images in the guanggao directory /". Next we will analyze the data according to the steps below.

1. First, create a MySQL database: guanggao

In Windows, it is easy to create a MySQL Empty database, because each database is stored in a sub-directory under the MySQL/data/directory, therefore, you only need to create a new sub-directory named guanggao in this directory, and there is no table or data in the database.

2. Create a New advertisement program: addnewad. php3

Design Concept: first obtain the user's new advertisement information using the form, and then upload the linked image (banner) file to the server's adbanner directory using the PHP function copy; finally, write the image file name, advertisement URL, description, and weighting information to the created Mysql Data Table ad. This programCodeAs follows:

# Addnewad. php3 -- add new advertisement #

Add new advertisement

// Form is used to input new advertisement information and hand it to the following PHP program for processing;
// The larger the weighted value priority, the higher the probability of loading the page image. The default value is 1.

Advertisement banner:

Advertisement URL:

Image Link Description alt:

Show weighting:

// PHP program for processing form data;
// The banner name and link address of the image cannot be blank;
If (banner! = "") & (URL! = "")){
// If the ad link and image name have been used, you must select another one;
If (file_exists ("adbanner/". banner_name )){
Echo "ad image. banner_name. Used. Please select another one! ";
Exit;
};
// Upload the linked image file name to the adbanner directory;
Copy (banner, "adbanner/". banner_name );
// Connect to the MySQL database;
Connection = mysql_connect ();
// Select the data source guanggao and execute the query ;;
Mysql_select_db ("guanggao ",);
Query = "select * from ad ";
Result = mysql_query (query );
// If the data table ad does not exist, create a new data table ad in the following structure;
If (! Result ){
Mysql_query ("
Create Table AD (
URL varchar (100) not null,
Banner varchar (150) not null,
Alt varchar (100) null,
Priority tinyint (4) default '1' not null
) ") Or die (mysql_error ());
}
// Insert new data from the form into the ad table;
Query = "insert into ad (URL, banner, ALT, priority) values ('url', 'banner_name ','
Alt ', 'priority ')";
// If the insert operation is successful, the following information is displayed;
Try = mysql_query (query );
If (try ){
Echo "An advertisement is added. Details:
";
Echo"
";
Echo "advertisement URL: URL
Ad Link Description: ALT
Show weighting: Priority ";
}
}
?>

3. Create a display advertisement program: showad. php3

Design Concept: obtain the information of all columns in the database table and store them in the corresponding one-dimensional array. Add the weights of all ads to get the maximum random number. Each time you load the page, use srand () generate a random tree seed, use the rand () function to generate a random number from 1 to the maximum random number, and then randomly display different advertising lines according to certain rules, the larger the weight, the larger the chance to be displayed.

# Showad. php3 -- randomly display advertisements #
// Select a database for connection;
Connection = mysql_connect ();
Mysql_select_db ("guanggao", connection );
// Execute the query to obtain the number of ads;
Query = "select URL, banner, ALT, priority from ad where priority> 0 ";
Result = mysql_db_query ("guanggao", query );
Numrows = mysql_num_rows (result );
// Use the mysql_fetch_object () function to obtain useful column information and store it in the corresponding array;
While (ROW = mysql_fetch_object (result )){
Adurl [] = row-> URL;
Adbanner [] = row-> banner;
Adalt [] = row-> ALT;
Adpriority [] = row-> priority;
}
// Initialize the intermediate variable;
Numcheck = numrows;
I = pricount = 0;
// Obtain the maximum random number;
While (numcheck ){
Pricount + = adpriority [I];
I ++; numcheck --;
}
// Random Seed is generated every one minute during program execution;
Srand (double) microtime () * 1000000 );
// Obtain a random number between 1 and the maximum random number;
PRI = rand (1, pricount );
// Clears the intermediate variable;
Pricount = 0;
// Different values are weighted to generate an array of elements used to display advertisements as strings;
For (I = 0; I <numrows-1; I ++ ){
Pricount + = adpriority [I];
If (PRI <= pricount ){
AD [] = "";
}
}
// Display the advertisement. The greater the weight, the greater the display opportunity;
Echo AD [0];
?>

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.