PHP Novice on the road (10)

Source: Internet
Author: User
Tags header min php file rand range
9. Simple banner Dynamic Replacement





do not know if you have found the major sites on the banner ads, each time we visit these sites, will see different advertising icon, or if you refresh the page every time, these ads banner will continue to 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):


;?


//generating random number


Srand (Double) microtime () *1000000);


//between 0 and 4 to take a number


$randval = rand (0,5);


//Display results


echo "<a href=//gophp.heha.net/index.html><img alt= into the world of PHP border=0 src=http://www.163design.net/p/a/ $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 get one of the random values, the last display http://www.163design.net/p/a /$randval. gif pictures banner, i.e. 0.gif, 1.gif, 2.gif, 3.gif, or 4.gif. For the sake of understanding, I list the syntax and related instructions for the RAND function as follows:


Rand


syntax: int rand ([int min], [int Max]);


return value: Integer


function Type: mathematical Operation


Content Description: This function is used to obtain random values. If you do not specify 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 one out of 10,000 random rate when executed





9.1 We change the Head.inc file to apply this simple banner dynamic replacement function, while also linking to their corresponding URLs for different ad banner.





of course, first we have to get ready to replace the Exchange banner icon, but also to our page header plus our own website logo (01dc.gif).





New Header file (header.inc):


;?


//Define General page head


?>


<HTML>


<HEAD>


<TITLE>.? echo "$MySiteName-$title";?> </TITLE>


<style type= "Text/css" >


<!--


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


-->


</style>


</HEAD>


<body topmargin=2>


<table width= "100%" border= "0" >


<tr>


<td rowspan= "3" width= "19%" ><img src=http://www.163design.net/p/a/"Http://gophp.heha.net/test/images" /01dc.gif "width=" 140 "height=" ></td>


<td rowspan= "3" width= "29%" >


;?


//Get the number of random seeds


Srand (Double) microtime () *1000000);


//between 0 and 4 to take a number


$randval = rand (0,5);


//Display results


switch ($randval)


{


case "0";


echo "<a href=//gophp.heha.net/index.html><img border=0 src=http://www.163design.net/p/a/$randval. gif ></a> ";


break;


case "1";


echo "<a href=//personal.668.cc/haitang/index.htm><img border=0 src=http://www.163design.net/p/a/$ Randval.gif></a> ";


break;


Case "2";


echo "<a href=//gophp.heha.net/index.html><img border=0 src=http://www.163design.net/p/a/$randval. gif ></a> ";


break;


case "3";


echo "<a href=//gophp.heha.net/index.html><img border=0 src=http://www.163design.net/p/a/$randval. gif ></a> ";


break;


case "4";


echo "<a href=//personal.668.cc/haitang/index.htm><img border=0 src=http://www.163design.net/p/a/$ Randval.gif></a> ";


break;


}


?>


</td>


<td width= "52%" >


<div align= "center" ><a href=. /test/form.php3 "class=" text > Automatically send a message test </a></div>


</td>


</tr>


<tr>


<td width= "52%" >


<div align= "center" ><a href= ". /test/php/php1.php3 "class=" text "> Simple recurrent advertising replacement </a></div>


</td>


</tr>


<tr>


<td width= "52%" >


<div align= "center" ><a href=. /password/password.php3 "class=" text > Simple password validation instance </a></div>


</td>


</tr>


</table>


<hr color= "#ff9900" size= "4" >


</body>


</html>








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.