PHP Beginners Basics (ix)

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags .net advertising basics beginners change class different echo

A simple interactive Web site example-Simple banner dynamic replacement (vi)

9. Simple Banner Dynamic replacement

I do not know if you have found the major sites on the banner ads banner, each time we visit these sites, will see a different advertising icon, or if you refresh the page every time, these ads banner will constantly change the random replacement. To achieve this effect although the use of JavaScript can also be achieved (like the Tenkine station of the dynamic transformation of advertising banner is by invoking JavaScript to achieve), but if we use PHP, we can also combine the database to do a large amount of data, such as a daily problem such as the function. Crap, let's take a look at how PHP can be used to achieve banner dynamic replacement function.

Simple banner Dynamic replacement PHP file (BANNER.PHP3):
?
Generate random numbers
Srand (Double) microtime () *1000000);
Take a number between 0 and 4
$randval = rand (0,5);
Show results
echo "<a href=//gophp.heha.net/index.html><img alt= into the PHP world border=0 src= $randval .gif></a>";
?>

We can find that the implementation of the program is very simple: the main thing is to use the Srand initialization random number generator to generate random numbers, and then call the RAND function within the defined valid range to obtain one of the random values, and finally display $randval.gif each picture banner, that is 0.gif, 1. GIF, 2.gif, 3.gif, or 4.gif. For the sake of understanding, I list the syntax and instructions for the RAND function as follows:
Rand
Syntax: int rand ([int min], [int Max]);
return value: Integer
Types of functions: mathematical operations
Description: This function is used to obtain random values. Without specifying the maximum and minimum range of random numbers, this function automatically takes a random number from 0 to Rand_max. If a parameter is specified for min and Max, a number is taken from the specified parameter. For example, Rand (38,49) obtains a random value from 38 to 49. Where the UNIX system contains the 49,WIN32 system does not contain 49. It is noteworthy that in order to maximize the random number of random numbers, it is best to use Srand () to set new random numbers each time before taking random numbers. In this example, the time factor is added when a new random number is produced with Srand (), and the random number is generated at a random rate of one out of 10,000 when executed.

9.1 We change the Head.inc file to apply this simple banner dynamic replacement function, but also for different advertising banner link to their corresponding URLs.

Of course, first we have to prepare the banner icon to replace the exchange, but also to our page header plus our own website logo (01dc.gif).

New Header file (header.inc):





//Define General page head


?&gt;


&lt;HTML&gt;


&lt;HEAD&gt;


&lt;TITLE&gt; echo "$MySiteName-$title";?&gt; &lt;/TITLE&gt;


&lt;style type= "Text/css" &gt;


&lt;!--


. text {font-family: "Song body"; font-size:12pt color: #006633; Text-decoration:none}


--&gt;


&lt;/style&gt;


&lt;/HEAD&gt;


&lt;body topmargin=2&gt;


&lt;table width= "100%" border= "0" &gt;


&lt;tr&gt;


&lt;td rowspan= "3" width= "19%" &gt;&lt;img "src=" "Http://gophp.heha.net/test/images/01dc.gif" width= "140" &gt;&lt;/td&gt;


&lt;td rowspan= "3" width= "29%" &gt;





//Get the number of random seeds


Srand (Double) microtime () *1000000);


//Between 0 and 4 take a number


$randval = rand (0,5);


//Display results


switch ($randval)


{


case "0";


echo "&lt;a href=//gophp.heha.net/index.html&gt;&lt;img border=0 src= $randval .gif&gt;&lt;/a&gt;";


break;


case "1";


echo "&lt;a href=//personal.668.cc/haitang/index.htm&gt;&lt;img border=0 src= $randval .gif&gt;&lt;/a&gt;";


break;


case "2";


echo "&lt;a href=//gophp.heha.net/index.html&gt;&lt;img border=0 src= $randval .gif&gt;&lt;/a&gt;";


break;


case "3";


echo "&lt;a href=//gophp.heha.net/index.html&gt;&lt;img border=0 src= $randval .gif&gt;&lt;/a&gt;";


break;


case "4";


echo "&lt;a href=//personal.668.cc/haitang/index.htm&gt;&lt;img border=0 src= $randval .gif&gt;&lt;/a&gt;";


break;


}


?&gt;


&lt;/td&gt;


&lt;td width= "52%" &gt;


&lt;div align= "center" &gt;&lt;a href=. /test/form.php3 "class=" text &gt; Automatically send a message test &lt;/a&gt;&lt;/div&gt;


&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td width= "52%" &gt;


&lt;div align= "center" &gt;&lt;a href=. /test/php/php1.php3 "class=" text "&gt; Simple recurrent advertising replacement &lt;/a&gt;&lt;/div&gt;


&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td width= "52%" &gt;


&lt;div align= "center" &gt;&lt;a href=. /password/password.php3 "class=" text &gt; Simple password validation instance &lt;/a&gt;&lt;/div&gt;


&lt;/td&gt;


&lt;/tr&gt;


&lt;/table&gt;


&lt;hr color= "#ff9900" size= "4" &gt;


&lt;/body&gt;


&lt;/html&gt;


Related Article

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.