Build a simple interactive website (VI)
9. Dynamic replacement of simple banner
I wonder if you have found header advertising banners on major sites. Every time we visit these sites, we will see different advertising icons, or if you refresh the page, these advertising banners will be changed and changed randomly. Although it can be achieved with JavaScript (the dynamic transformation of advertising banner on the Tianji website is achieved by calling JavaScript), if we use PHP, we can also combine the database to create a large amount of data, such as a daily question. Let's take a look at how to use PHP to implement the dynamic replacement function of banner.
Simple banner dynamically replaces PHP files (banner.php3):
<?
//Create random number
srand((double)microtime()*1000000);
//Take a number between 0 and 4
$randval = rand(0,5);
// Show results
echo "<a href=///><img alt=enter the world of php border=0 src=$></a>";
?>
We can find that the implementation program is very simple: we mainly use srand, which initializes the random number generator to generate random numbers, then call the rand function to obtain one of the random values within the defined valid range, and finally display the $size banner, that is,,,, or. To facilitate everyone's understanding, I will list the syntax and related instructions of the rand function as follows:
rand
Syntax: int rand([int min], [int max]);
Return value: integer
Function type: mathematical operations
Content description: This function is used to obtain random values. If the maximum and minimum range of random numbers are not specified, this function will automatically take a random number from 0 to RAND_MAX. If there are parameters for specifying min and max, take a number from the specified parameters. For example, rand(38,49) will get a random value from 38 to 49. Among them, the UNIX system contains 49, and the Win32 system does not contain 49. It is worth noting that in order to maximize the randomness of a random number, it is best to use srand() to set a new random number before taking the random number each time. In this case, when using srand() to generate a new random number, a time factor is added, and the random number is generated with a random rate of one million during execution.
9.1 We change the file to apply the simple banner dynamic replacement function, and we also have to link to their corresponding URLs for different advertising banners.
Of course, first of all, we must prepare the banner icon used to replace the exchange, and also add the logo of our own website () to our page header.
New header file():
<?
// Define the header of the common page
?>
<HTML>
<HEAD>
<TITLE> <? echo "$MySiteName - $title"; ?> </TITLE>
<style type="text/css">
<!--
.text { font-family: "Stick"; 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="/test/images/" width="140" height="60"></td>
<td rowspan="3" width="29%">
<?
//Get random seeds
srand((double)microtime()*1000000);
//Take a number between 0 and 4
$randval = rand(0,5);
// Show results
switch($randval)
{
case "0";
echo "<a href=///><img border=0 src=$></a>";
break;
case "1";
echo "<a href=//personal./haitang/><img border=0 src=$></a>";
break;
case "2";
echo "<a href=///><img border=0 src=$></a>";
break;
case "3";
echo "<a href=///><img border=0 src=$></a>";
break;
case "4";
echo "<a href=//personal./haitang/><img border=0 src=$></a>";
break;
}
?>
</td>
<td width="52%">
<div align="center"><a href="../test/form.php3" class="text">Automatic send email test</a></div>
</td>
</tr>
<tr>
<td width="52%">
<div align="center"><a href="../test/php/php1.php3" class="text">Simple reincarnation advertising replacement</a></div>
</td>
</tr>
<tr>
<td width="52%">
<div align="center"><a href="../password/password.php3" class="text">Simple password verification example</a></div>
</td>
</tr>
</table>
<hr color="#ff9900" size="4">
</body>
</html>
9. Dynamic replacement of simple banner
I wonder if you have found header advertising banners on major sites. Every time we visit these sites, we will see different advertising icons, or if you refresh the page, these advertising banners will be changed and changed randomly. Although it can be achieved with JavaScript (the dynamic transformation of advertising banner on the Tianji website is achieved by calling JavaScript), if we use PHP, we can also combine the database to create a large amount of data, such as a daily question. Let's take a look at how to use PHP to implement the dynamic replacement function of banner.
Simple banner dynamically replaces PHP files (banner.php3):
<?
//Create random number
srand((double)microtime()*1000000);
//Take a number between 0 and 4
$randval = rand(0,5);
// Show results
echo "<a href=///><img alt=enter the world of php border=0 src=$></a>";
?>
We can find that the implementation program is very simple: we mainly use srand, which initializes the random number generator to generate random numbers, then call the rand function to obtain one of the random values within the defined valid range, and finally display the $size banner, that is,,,, or. To facilitate everyone's understanding, I will list the syntax and related instructions of the rand function as follows:
rand
Syntax: int rand([int min], [int max]);
Return value: integer
Function type: mathematical operations
Content description: This function is used to obtain random values. If the maximum and minimum range of random numbers are not specified, this function will automatically take a random number from 0 to RAND_MAX. If there are parameters for specifying min and max, take a number from the specified parameters. For example, rand(38,49) will get a random value from 38 to 49. Among them, the UNIX system contains 49, and the Win32 system does not contain 49. It is worth noting that in order to maximize the randomness of a random number, it is best to use srand() to set a new random number before taking the random number each time. In this case, when using srand() to generate a new random number, a time factor is added, and the random number is generated with a random rate of one million during execution.
9.1 We change the file to apply the simple banner dynamic replacement function, and we also have to link to their corresponding URLs for different advertising banners.
Of course, first of all, we must prepare the banner icon used to replace the exchange, and also add the logo of our own website () to our page header.
New header file():
<?
// Define the header of the common page
?>
<HTML>
<HEAD>
<TITLE> <? echo "$MySiteName - $title"; ?> </TITLE>
<style type="text/css">
<!--
.text { font-family: "Stick"; 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="/test/images/" width="140" height="60"></td>
<td rowspan="3" width="29%">
<?
//Get random seeds
srand((double)microtime()*1000000);
//Take a number between 0 and 4
$randval = rand(0,5);
// Show results
switch($randval)
{
case "0";
echo "<a href=///><img border=0 src=$></a>";
break;
case "1";
echo "<a href=//personal./haitang/><img border=0 src=$></a>";
break;
case "2";
echo "<a href=///><img border=0 src=$></a>";
break;
case "3";
echo "<a href=///><img border=0 src=$></a>";
break;
case "4";
echo "<a href=//personal./haitang/><img border=0 src=$></a>";
break;
}
?>
</td>
<td width="52%">
<div align="center"><a href="../test/form.php3" class="text">Automatic send email test</a></div>
</td>
</tr>
<tr>
<td width="52%">
<div align="center"><a href="../test/php/php1.php3" class="text">Simple reincarnation advertising replacement</a></div>
</td>
</tr>
<tr>
<td width="52%">
<div align="center"><a href="../password/password.php3" class="text">Simple password verification example</a></div>
</td>
</tr>
</table>
<hr color="#ff9900" size="4">
</body>
</html>