显示广告列表(1)
广告管理程序的核心是广告页面展示的部分,它的作用相对一个引导页面所有的操作,都是从看到广告列表的显示开始发起的。首先来看页面列表显示部分的程序实现,代码如下:
广州网站设计
- /*******************************************
- '文件名:index.php
- '主要功能:广告管理模块index
- '说明:
- '*******************************************/
- include_once(dirname(__FILE__)."/inc/include.smarty.php");
- //加载Smarty配置文件
- include(dirname(__FILE__). "/inc/include.function.php"); //加载函数库文件
- require(dirname(__FILE__)."/config/config.php"); //加载容配置文件
- $type=$_REQUEST[type]; //获得操作类型
- $kw=$_REQUEST[kw];
- $page=$_REQUEST[page]; //获得当前页数
- if($page<1){
- $page=1;
- }
- $page=$page-1;
- $msgd=explode("|www_allen|",file_get_contents($dbtable)); //切割字符串
- $rows=count($msgd); //计算行数
- if($kw){
- for ($i=0;$i<=$rows;$i++){
- $tmp2=explode("|allen|",$msgd[$i]);
- if($type=="name"){ //判断操作类型(name)
- $msgn[$i]=$tmp2[0];
- }elseif($type=="nr"){
- $msgn[$i]=$tmp2[2];
- }
- elseif($type=="gge"){ //类型判断(gge)
- $msgn[$i]=$tmp2[4];
- }
- else{$msgn[$i]=$tmp2[3];
- }
- if(eregi($kw,$msgn[$i])){ //字符串比对解析
- $chj_nr[]=$msgd[$i];
- $chj_numib++;
- }
- }
- if($chj_numib<1){
- echo "没有相关 $kw 的匹配结果!!"; //输出报错信息
- exit;
- }
- $msg=array_reverse($chj_nr);
- $total=ceil($chj_numib/$pagesize); //计算总页面数
- $kwnum=-1;
- }else{
- $chj_numib=$rows;
- $msg=array_reverse($msgd);
- $total=ceil($chj_numib/$pagesize); //计算分页
- }
- //如果每页显示数*总页数小于留言总条数则
- if($pagesize*$total< $chj_numib){
- $total++; //总页面数加1
- }
- $total2=$total-1;
- $page2=$page+1;
- $pp=$page*$pagesize+1+$kwnum; //计算开始条数
- $pp2=$pp+$pagesize; //计算结尾条数
- $nextpage=$page+2; //下一个页面
- $prevpage=$page; //上一个页面
- $chj_adlen=$chj_numib-1;
- for($i=$pp;$i< $pp2;$i++){
- if($i < $chj_numib){
- $tmp=explode("|allen|",$msg[$i]);
- if(!empty($kw)){
- //有搜索值时
- $arr[$i]['title']=$tmp[0];
- $arr[$i]['type']=$tmp[1];
- $arr[$i]['size']=$tmp[4];
- $arr[$i]['desc']=$tmp[3];
- }else{
- //没有搜索时即显示全部
- $arr[$i-1]['title']=$tmp[0];
- $arr[$i-1]['type']=$tmp[1];
- $arr[$i-1]['size']=$tmp[4];
- $arr[$i-1]['desc']=$tmp[3];
- }
- }
- }
- $smarty->assign("kw", $kw);
- $smarty->assign("type", $type); //类型标签
- $smarty->assign("prevpage", $prevpage); //前一页标签
- $smarty->assign("nextpage", $nextpage); //下一页标签
- $smarty->assign("total2", $total2); //页面标签
- //总条数标签
- $smarty->assign("page2", "<font color=red>".$page2."</font>/".
- $total."");
- $smarty->assign("total", $chj_adlen); //总条数标签
- $smarty->assign("content", $arr); //生成广告内容标签
- $smarty->display("./index.html"); //加载首页模板
代码的执行流程步骤如下。
(1)加载配置文件(模板引擎配置文件、公共函数库、预定义配置参数)。
(2)判断操作的类型(是否为关键字搜索),使用$kw变量做流程控制。
(3)获取广告内容文件,使用explode()对预先定义的分割标签切割字符串,该条代码如下:
广州网站设计
- $msgd=explode("|www_allen|",file_get_contents($dbtable));
(4)在通过循环遍历,将数组中的字符串取出处理后的再次切割为"内容",处理过程代码如下:
广州网站建设
- for($i=$pp;$i< $pp2;$i++){
- if($i < $chj_numib){
- $tmp=explode("|allen|",$msg[$i]); //切割字符
- if(!empty($kw)){ //判断是否为空
- $arr[$i]['title']=$tmp[0];
- $arr[$i]['type']=$tmp[1];
- $arr[$i]['size']=$tmp[4];
- $arr[$i]['desc']=$tmp[3];
- }else{
- //当为空时为前一个节点数组赋值
- $arr[$i-1]['title']=$tmp[0];
- $arr[$i-1]['type']=$tmp[1];
- $arr[$i-1]['size']=$tmp[4];
- $arr[$i-1]['desc']=$tmp[3];
- }
- }
- }
(5)得到结果数组之后生成Smarty模板标签,代码如下:
广州网站建设
- $smarty->assign("kw", $kw);
- $smarty->assign("type", $type);
- $smarty->assign("prevpage", $prevpage);
- $smarty->assign("nextpage", $nextpage);
- $smarty->assign("total2", $total2); //页面标签
- //总条数标签
- $smarty->assign("page2", "<font color=red>".$page2."</font>/".$total."");
- $smarty->assign("total", $chj_adlen); //总条数标签
- $smarty->assign("content", $arr); //生成广告内容标签
- $smarty->display("./index.html"); //加载首页模板



