Use PHP to implement small site advertisement management (revised version) _ PHP Tutorial

Source: Internet
Author: User
Tags random seed
Use PHP to manage small-sized website ads (revised version ). Today, I will follow the examples and find many errors. in order to give the Cainiao brothers a better understanding, I will show you the modified files. The data structure is as follows: CREAT follows the example today and finds many errors. to give the Cainiao brothers a better understanding, I will show you the modified file.
The data structure is as follows:

Create table ad (
Url varchar (100) not null,
Banner varchar (150) not null,
Alt varchar (1, 100 ),
Priority int (4) DEFAULT '1' not null,
)

Add advertisement files
* ************** Putad. php ********************

// 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;
Include ("config. inc ");
// Insert new data from the form into the ad table;
$ Query = "insert into ad (url, alt, priority, banner) values ('$ url',' $ alt ',' $ priority ',' $ banner_name ')";
// If the insert operation is successful, the following information is displayed;
$ Try = mysql_query ($ query );
If ($ try ){
Echo "an advertisement has been added. Details :";
Echo "";
Echo "advertisement url: $ url
Ad link description: $ alt
Show weighting: $ priority ";
} Else {echo "error ";}
}
} Else {
?>


Untitled



Ad Exchange table







* ************** Showad. php ********************


Include ("config. inc ");
$ Query = "SELECT url, banner, alt, priority from ad ";
$ Result = mysql_query ($ 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; $ I ++ ){
$ Pricount + = $ adpriority [$ I];
If ($ pri <= $ pricount ){
$ Ad [] = "";
}
}
// Display the advertisement. the greater the weight, the greater the display opportunity;
Echo $ ad [0];
?>


Note: config. inc is used to connect to the mysql database, which is not listed here. There is still room for improvement in the above program, and there are still many functions not implemented. you can modify them if you want. But it is better to give me a copy after modification: terence611005@sina.com
If you have any questions, contact me: oicq: 18680986.

Bytes. The data structure is as follows: CREAT...

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.