Small-sized website advertisement management with PHP _ PHP Tutorial

Source: Internet
Author: User
Tags random seed
Use PHP to manage small site ads. 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 the most common online advertisement mode for the 468x image link advertisement. However, 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, this avoids the disadvantages of developing FTP programs or plug-ins to implement this function in ASP and Perl CAI.

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. The program code is as 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];
?>

Bytes. The file upload function of PHP provides the file upload function in this section 468x...

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.