以上都不能实现数据库中表名Banner 有字段 id 自动编号 name 广告名 pic 广告所在地址及名称 例image\qj163.gif flash 是不是Flash文件 例:flash yenno 是否显示我在functions.php文件中已定意好了数据连接等,有以下可以打开其中一个图 $gq = $db->query("SELECT * FROM Banner WHERE Yesno='Y' order by id desc limit 1");while ($rs=$db->fetch_array($gq)) { echo <img src=$rs[Pic]"; }请教我要如何写才能产生随机广告?
把ID字段的值写到一个数组中,然后用随机数函数rand()求出数组中的其中一个ID值赋于变量$Mid,就可以 $gq = $db->query("SELECT * FROM Banner WHERE Yesno='Y' and ID=$Mid"); while ($rs=$db->fetch_array($gq)) { echo <img src=$rs[Pic]"; } 就OK啦。
2.利用 array_rand():srand ((float) microtime() * 10000000);//播种
echo array_rand(@a);//详细用法查手册
// 用法
// include("ranimage.php");
// 或者
// require "ranimage.php";
$dir = "";
function Get_Image_list($dir) {
if(!$dir) {
$dir = ".";
}
$file_array = array();
$dir_handle = opendir($dir);
$a = 0;
while($file = readdir($dir_handle)) {
if((preg_match('/jpg/',$file)) ||
(preg_match('/png/',$file)) ||
(preg_match('/gif/',$file)) ||
(preg_match('/jpeg/',$file))) {
$file_array[$a] = $file;
$a++;
}
}
return $file_array;
}
$files = Get_Image_list($dir);
$max = count($files)-1;
srand((double)microtime()*1000000);
$index = rand(0,$max);
$image_name = $files[$index];
echo("<IMG SRC="".$image_name."">");
?>
<?php
#########随机广告显示##########
function myads(){
$dir="ads"; #设置存放记录的目录
//$dir="ads"; #设置存放记录的目录
$ads="$dir/ads.txt"; #设置广告代码文件
$log ="$dir/ads.log"; #设置ip记录文件 $ads_lines=file($ads);
$lines=count($ads_lines);#文件总行数 ####读出广告总数$ads_count和显示次数到数组$display_array########
$ads_count=0;
$display_count=0;
for ($i=0;$i<$lines;$i++){
if((!strcmp(substr($ads_lines[$i],0,7),"display"))){
$ads_count+=1;
$display_array[$ads_count]=substr($ads_lines[$i],8);
$display_count+=$display_array[$ads_count];
}
}
####决定随机显示序号$display_rand#####
srand((double)microtime()*1000000);
$display_rand = rand(1,$display_count); ###决定广告序号$ads_num######
$pricount=0;
$ads_num=1;
for($i=1; $i<=$ads_count; $i++) {
$pricount += $display_array[$i];
if ($display_rand<=$pricount) {$ads_num=$i;break;}
} #####播放广告代码#########
$num=0;
$flag=0; for($i=0;$i<$lines;$i++){
if((!strcmp(substr($ads_lines[$i],0,7),"display"))){$num++;}
if(($num==$ads_num)and($flag==0)){$flag=1;continue;}
if(($flag==1)and strcmp($ads_lines[$i][0],"#")){echo $ads_lines[$i];continue;}
if(($flag==1)and(!(strcmp($ads_lines[$i][0],"#")))){break;}
}
####纪录广告显示次数#########
$fp=fopen($log,"a");
fputs($fp,date( "Y-m-d H:i:s " ).getenv("REMOTE_ADDR")."==>".$ads_num."\n");
fclose($fp);
}
?> 广告代码文件ads.txt ########每个广告代码之间用'#'隔开,display为显示加权数,越大显示次数越多################
################################
display=10 <a href="http://www.5548.net/">
<img src="http://www.5548.net/images/5548.gif" alt="欢迎光临5548设设联盟!"> </a>
################################
display=10 <a href="http://www.aoshen.com/" target=_blank>
<img src="http://www.aoshen.com/images/a-043.gif" width="172" height="64" alt="欢迎光临澳深网!" border="0"></a>
调用<?php myads();?>即可
$query = mysql_query($sql,$db);
$nums = mysql_num_rows($query);
if($nums > 0)
{
$i = 0;
$nums1 = $nums - 1;
$nums2 = rand(0,$nums1);
while($row2 = mysql_fetch_array($query))
{
if($i == $nums2)
{
$media = $row["field"];
}
$i++;
}
$media就是你想要的图片或者是flash文件
有字段
id 自动编号
name 广告名
pic 广告所在地址及名称 例image\qj163.gif
flash 是不是Flash文件 例:flash
yenno 是否显示我在functions.php文件中已定意好了数据连接等,有以下可以打开其中一个图
$gq = $db->query("SELECT * FROM Banner WHERE Yesno='Y' order by id desc limit 1");while ($rs=$db->fetch_array($gq)) {
echo <img src=$rs[Pic]";
}请教我要如何写才能产生随机广告?
$gq = $db->query("SELECT * FROM Banner WHERE Yesno='Y' and ID=$Mid");
while ($rs=$db->fetch_array($gq)) {
echo <img src=$rs[Pic]";
}
就OK啦。