Implement small site ad management with PHP (Revised edition)

Source: Internet
Author: User
Tags array config implement include insert mysql query mysql database
Do according to the example today, found that there are many mistakes in the place, in order to let the vast number of rookie brother better understanding, I put the revised document to everyone to see.
The data structure is as follows:

CREATE TABLE AD (
URL varchar not NULL,
Banner varchar () not NULL,
ALT varchar (100),
priority int (4) DEFAULT ' 1 ' not NULL,
)

Add ads to the file
putad.php********************

? if ($submit) {
PHP program for processing form data;
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;
Include ("Config.inc");
Inserts new data from the form into the datasheet ad;
$query = "INSERT into AD (URL, alt, priority, banner) VALUES (' $url ', ' $alt ', ' $priority ', ' $banner _name ')";
Insert Success Displays the following information;
$try =mysql_query ($query);
if ($try) {
echo "A new ad is completed, details:";
echo "";
echo "AD site: $url
Advertising Link Description: $alt
Display weighting: $priority ";
}else{echo "Error";}
}
}else{
?>
<title>Untitled</title>

<body bgcolor= "#ffffff" >
<p> Advertising exchange Table </p>

<form method= "POST" action= "putad.php" enctype= "Multipart/form-data" >
<p> Image URL:
<input type= "file" name= "banner" >
</p>
<p> Connection URL:
<input type= "text" name= "url" >
</p>
<p> Display Weights:
<input type= "text" name= "priority" ></p>
<p> Connection Instructions:
<input type= "text" name= "alt" >

</p>

<p>
<input type= "Submit" name= "Submit" value= "OK" >
<input type= "reset" name= "Concel" value= "refill" >
</p>
</form>
</body>

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 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; $i + +) {
$pricount + + $adpriority [$i];
if ($pri <= $pricount) {
$ad []=] <a href= $adurl [$i]></a> ";
}
}
Displays the advertisement, the weight value is bigger, displays the opportunity to be bigger;
echo $ad [0];
?>


Note: Config.inc inside is the thing that connects the MySQL database, in this is not listed. The above program also has the improvement space, but also has many functions not to realize, if you are willing you also may revise.

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.