6;i = 0 ; $i < count ( $ad ); $i ++) {
$pricount += $ad [ $i ][ 3 ];
if ( $pri <= $pricount ) {
$ad1 []= "<a href="http://www.dvbbs.net/tech/php/. $ad [ $i ][ 0 ]." target=new><img src=http://www.dvbbs.net/ad/" . $ad [ $i ][ 1 ]. " width=468 height=60 border=0 alt=\"" . $ad [ $i ][ 2 ]. "\"></a>" ;
}
}
echo $ad1 [ 0 ];
?>
$pricount += $ad [ $i ][ 3 ];
if ( $pri <= $pricount ) {
$ad1 []= "<a href="http://www.dvbbs.net/tech/php/. $ad [ $i ][ 0 ]." target=new><img src=http://www.dvbbs.net/ad/" . $ad [ $i ][ 1 ]. " width=468 height=60 border=0 alt=\"" . $ad [ $i ][ 2 ]. "\"></a>" ;
}
}
echo $ad1 [ 0 ];
?>
上面的程序为公用的广告显示程序,其中的 $pricount 变量为所有广告 priority 加起来的和。程序先将所有的广告信息读到数组变量 $ad 中,随即关上数据库。再依时间取随机数种子,之后再从 1 到 $pricount 间随机取一个数字。
网页中要用广告程序,只要在需要广告的地方加上 <? include( "ad.php" ); ?> 就可以了,当然 include 的路径 (在 httpd.conf 中) 要先设好才行。
上面的程序还有改进空间,可以加入广告的 click log 功能,或是显示的 log 功能,改动显示加权的程序....等等,就不做范例了,毕竟在这儿是要介绍 php 的实际应用及程序开发,而不是套件开发。真的需要现成的广告套件,不妨到 http://www.phpwizard.net/phpads ,这是一套用 php 开发出来的广告程序。
| 对此文章发表了评论 |
