Implement small site advertisement management with PHP

Source: Internet
Author: User
Tags array empty file upload implement insert mysql php file mysql database
Advertising | site Picture link advertising is probably the most commonly used online advertising mode, but the dynamic random display of multiple ads is not so simple things. PHP's File Upload function provides a 468x60 pixel area (the standard size of web ads) to achieve a random display of advertising based on the size of the weight of the solution, thereby avoiding the development of FTP programs or ASP and Perl CAI need to plug extensions to achieve this functionality.

This example is debugged under Php4+mysql+apache for win9x/2000, which includes two parts: The program used to add new ads ADDNEWAD.PHP3 and the program that displays the ads showad.php3, stored in the server PHP file directory. test/guanggao/"directory. Create a subdirectory "adbanner/" in the Guanggao directory that holds the advertisement link picture. Below we follow the steps to analyze.

1. First, establish MySQL database: Guanggao

In a Windows environment, it's easy to build a MySQL-empty database because each database is a subdirectory in the mysql/data/directory, so just create a new subdirectory in that directory Guanggao, and there are no tables and no data in the database.

2. Create new advertising program: ADDNEWAD.PHP3

Design idea: The first use form to get the user's new advertising information, and then use the PHP function copy () to upload the linked picture (banner) file to the server Adbanner directory, and finally the image file name, advertising site, description, weighting information, such as the creation of the MySQL data table AD. The program code is as follows:

# addnewad.php3--to add new ads #
<center>The form is used to enter the new advertising information and give it to the following PHP program;
Among them, the larger the weighted value priority number, the higher the probability of loading the page picture, the default is 1.
<form enctype= "Multipart/form-data" action= "Addnewad.php3" method=post>
Advertising Banner: <input name=banner type= "file" ><br>
Advertising URL: <input name=url type=text size=30><br>
Picture Link Description alt: <input Name=alt type=text size=30><br>
Display weighting: <input name=priority type=text size=5 value=1><br>
<input type= "Submit" value= "submitted" >
</FORM>
PHP program for processing form data;
<?php
The picture banner name and the link address cannot be empty;
if ((banner!= "") & (url!= "")) {
If the advertisement link and the picture name have already been used, must choose another;
if (file_exists ("adbanner/". Banner_name)) {
echo "Advertising picture. Banner_name. has been used, please choose another! ";
Exit
};
Upload link picture filename to adbanner directory;
Copy (banner, "adbanner/". Banner_name);
Connect MySQL database;
Connection = mysql_connect ();
Select data source Guanggao, execute query;;
mysql_select_db ("Guanggao",);
Query= "SELECT * from AD";
Result=mysql_query (query);
If the datasheet ad does not exist, create a new datasheet ad in the following structure;
if (!result) {
mysql_query ("
CREATE TABLE AD (
URL varchar NOT NULL,
Banner varchar () NOT NULL,
Alt varchar (MB) NULL,
Priority tinyint (4) default 1 NOT NULL
) or Die (Mysql_error ());
}
Inserts new data from the form into the datasheet ad;
query= "INSERT into AD (URL, banner, ALT, priority) values (URL, banner_name,
ALT, priority) ";
Insert Success Displays the following information;
Try=mysql_query (query);
if (try) {
echo "an ad is added to the complete, details:
";
echo "<a href=" url "></a><br>";
echo "Advertising URL: url<br> ads link Description: alt<br> Display weighted: priority";
}
}
?>

3. Establish the Display advertising program: SHOWAD.PHP3

Design idea: Get all the column information in the database table to be stored in the corresponding one-dimensional array, add the weights of all ads to a maximum random number, each time the page is loaded with Srand () to produce a random tree seed, and then the rand () function produces a random number from 1 to the maximum random number, Then according to a certain rule randomly display different advertising bars, the greater the weight of the opportunity to show greater.

# showad.php3--ads randomly displayed #
<?php
Connection selection database;
Connection = mysql_connect ();
mysql_select_db ("Guanggao", connection);
Execute the query to get 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 coexist in the corresponding array;
while (row = mysql_fetch_object (result)) {
adurl[]= row->url;
adbanner[]= row->banner;
adalt[]= row->alt;
adpriority[]= row->priority;
}
initialization of intermediate variables;
Numcheck= NumRows;
I= pricount=0;
The maximum random number is obtained.
while (Numcheck) {
pricount+= adpriority[i];
i++; numcheck--;
}
A random number of seeds is generated in one out of 10,000 seconds when the program executes;
Srand (Double) microtime () *1000000);
A random number between 1 and the maximum random number is obtained;
pri = rand (1, pricount);
Middle variable clear 0;
pricount=0;
An array of elements that are used to display advertisements, depending on the weighted value;
for (i=0; i< numrows-1; i++) {
Pricount + + adpriority[i];
if (pri <= pricount) {
ad[]= "<a href=" adurl[i] "target=new></a>";
}
}
Displays the advertisement, the weight value is bigger, displays the opportunity to be bigger;
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.