一聚教程网:一个值得你收藏的教程网站

热门教程

php利用模板分页程序(带demo演示);

时间:2022-06-24 18:30:27 编辑:袖梨 来源:一聚教程网

//page.class.php


class page{

 var $currentpage;
 var $leftoffset;
 var $rightoffset;
   
 var $totalpage;//总页数
 var $recordcount;//总记录数
 var $pagesize;//每页显示条数

 var $pageurl;
 var $hypelink;

 var $template;
 var $tpl;
 var $tagitems=array();
 var $tagvalues=array();

 var $sqlquery;

 //构造函数
 function page($currentpage=1,$pagesize=5,$leftoffset=2,$rightoffset=7,$pageurl="?page="){
  echo "分页类开始";
  $this->currentpage=ceil(abs(@$currentpage+0));
  (empty($this->currentpage))?$this->currentpage=1:$this->currentpage=$this->currentpage;
  $this->pagesize=ceil(abs(@$pagesize+0));
  (empty($this->pagesize))?$this->pagesize=5:$this->pagesize=$this->pagesize;
  $this->leftoffset=ceil(abs(@$leftoffset+0));
  (empty($this->leftoffset))?$this->leftoffset=2:$this->leftoffset=$this->leftoffset;
  $this->rightoffset=ceil(abs(@$rightoffset+0));
  (empty($this->rightoffset))?$this->rightoffset=7:$this->rightoffset=$this->rightoffset;
  $this->pageurl=$pageurl;

  $this->setdefaulttagvalue();
 }
   
 //取得记录总数
 //$sql="select count(id) as n from table";
 function getrecordcount($sql,$conn){
  $query=@mysql教程_query($sql,$conn);
  if(!$query){echo "执行sql语句失败";exit();}
  while($rs=mysql_fetch_row($query)){
   $this->recordcount=$rs[0];//取得记录总数
  }
  $this->totalpage=ceil($this->recordcount / $this->pagesize);//计算总页数
  if($this->currentpage > $this->totalpage){$this->currentpage=$this->totalpage;}//判断当前页是否大于总页数
  mysql_free_result($query);
 }
   
 //select * from tb p->setlimit();
 function setlimit(){
  $limit="limit ".($this->currentpage-1)*$this->pagesize;
  $limit.=",$this->pagesize";
  return $limit;
 }
 
 function executesql($sql,$conn){
  if(!$sql||!$conn){echo "参数传递错误";return false;}
     $this->sqlquery=mysql_query($sql,$conn);
     if(!$this->sqlquery){echo "执行sql语句失败";return false;}
 }
 function recordset(){
  return mysql_fetch_array($this->sqlquery);
 }
   
 //取得模板内容
 function gettemplate($filedir){
  if(file_exists($filedir)){
   $f=fopen($filedir,"r");
   $this->template=fread($f,filesize($filedir));
  }else{
   echo "获取模板文件失败...文件不存在";
   exit();
  }
  //取得区块内容
  $start=strpos($this->template,"");
  $end=strpos($this->template,"");
  $this->tpl=substr($this->template,$start+strlen(""),$end-$start-strlen("")-2);
  if($this->tpl==""){echo "模板内容为空,请检查标签设置是否正确。";exit();}
  //echo $this->tpl;
 }

 

    
 //设定默认标签对应值
 function setdefaulttagvalue(){
  $this->tagitems["previouspage"]="上一页";
  $this->tagitems["previouspagelink"]="上一页";
  $this->tagitems["previoustenpage"]="上十页";
  $this->tagitems["previoustenpagelink"]="上十页";
  $this->tagitems["nextpage"]="下一页";
  $this->tagitems["nextpagelink"]="下一页";
  $this->tagitems["nexttenpage"]="下十页";
  $this->tagitems["nexttenpagelink"]="下十页";
  $this->tagitems["firstpage"]="首页";
  $this->tagitems["firstpagelink"]="首页";
  $this->tagitems["lastpage"]="尾页";
  $this->tagitems["lastpagelink"]="尾页";
  $this->tagitems["listpage"]="[{list}]";
  $this->tagitems["listpagelink"]="[{list}]";
       
  //定义模板标签
  $this->tagvalues["previouspage"]="{previouspage}";
  $this->tagvalues["previouspagelink"]="{previouspage}";
  $this->tagvalues["previoustenpage"]="{previoustenpage}";
  $this->tagvalues["previoustenpagelink"]="{previoustenpage}";
  $this->tagvalues["nextpage"]="{nextpage}";
  $this->tagvalues["nextpagelink"]="{nextpage}";
  $this->tagvalues["nexttenpage"]="{nexttenpage}";
  $this->tagvalues["nexttenpagelink"]="{nexttenpage}";
  $this->tagvalues["firstpage"]="{firstpage}";
  $this->tagvalues["firstpagelink"]="{firstpage}";
  $this->tagvalues["lastpage"]="{lastpage}";
  $this->tagvalues["lastpagelink"]="{lastpage}";
  $this->tagvalues["listpage"]="{list}";
  $this->tagvalues["listpagelink"]="{list}";
  /*其他标签直接替换
  {$datacount}:共{$datacount}条记录
  {$currentpage}:当前为第{$currentpage}页
  {$totalpage}:共{$totalpage}页
  {$numperpage}:每页{$numperpage}条
  */
 }
 // 重新设定标签对应值
 function settagvalue($item,$itemvalue="",$value=""){
  if(!isset($item)||!isset($itemvalue)||!isset($value)){return;}
  foreach($this->tagitems as $key=>$v){
   if($key==$item){
    (empty($itemvalue))?"":$this->tagitems[$key]=$itemvalue;//如果为空,则不改变
    (empty($value))?"":$this->tagvalues[$key]=$value;
   }
  }
 }

 

 //模板解析
 function prasetemplate(){
  //------a_begin------//
  if($this->totalpage > 1){
   //------b_begin------//
   if($this->currentpage > 1){
    //首页
    $t=str_replace("{link}",$this->pageurl."1",$this->tagitems["firstpagelink"]);
    $this->tpl=str_replace($this->tagvalues["firstpagelink"],$t,$this->tpl);
    //前一页
    $t=str_replace("{link}",$this->pageurl.($this->currentpage-1),$this->tagitems["previouspagelink"]);
    $this->tpl=str_replace($this->tagvalues["previouspagelink"],$t,$this->tpl);
    //------c_begin------//
    if($this->currentpage < $this->totalpage){
     //下一页
     $t=str_replace("{link}",$this->pageurl.($this->currentpage+1),$this->tagitems["nextpagelink"]);
     $this->tpl=str_replace($this->tagvalues["nextpagelink"],$t,$this->tpl);
     //尾页
     $t=str_replace("{link}",$this->pageurl.$this->totalpage,$this->tagitems["lastpagelink"]);
     $this->tpl=str_replace($this->tagvalues["lastpagelink"],$t,$this->tpl);
    }else{
     //下一页
     $this->tpl=str_replace($this->tagvalues["nextpage"],$this->tagitems["nextpage"],$this->tpl);
     //尾页
     $this->tpl=str_replace($this->tagvalues["lastpage"],$this->tagitems["lastpage"],$this->tpl);
    }
    //------c_end------//
   }else{
    //首页
    $this->tpl=str_replace($this->tagvalues["firstpage"],$this->tagitems["firstpage"],$this->tpl);
    //前一页
    $this->tpl=str_replace($this->tagvalues["previouspage"],$this->tagitems["previouspage"],$this->tpl);
    //下一页
    $t=str_replace("{link}",$this->pageurl.($this->currentpage+1),$this->tagitems["nextpagelink"]);
    $this->tpl=str_replace($this->tagvalues["nextpagelink"],$t,$this->tpl);
    //尾页
    $t=str_replace("{link}",$this->pageurl.$this->totalpage,$this->tagitems["lastpagelink"]);
    $this->tpl=str_replace($this->tagvalues["lastpagelink"],$t,$this->tpl);
   }
   //------b_end------//
  }else{
   //解析前一页,前十页,后一页,后十页,首页,尾页
   $this->tpl=str_replace($this->tagvalues["previouspage"],$this->tagitems["previouspage"],$this->tpl);
   $this->tpl=str_replace($this->tagvalues["previoustenpage"],$this->tagitems["previoustenpage"],$this->tpl);
   $this->tpl=str_replace($this->tagvalues["nextpage"],$this->tagitems["nextpage"],$this->tpl);
   $this->tpl=str_replace($this->tagvalues["nexttenpage"],$this->tagitems["nexttenpage"],$this->tpl);
   $this->tpl=str_replace($this->tagvalues["firstpage"],$this->tagitems["firstpage"],$this->tpl);
   $this->tpl=str_replace($this->tagvalues["lastpage"],$this->tagitems["lastpage"],$this->tpl);
  }
  //------a_end------//

  //前十页,后十页
  if($this->currentpage-10>=1){
   $t=str_replace("{link}",$this->pageurl.($this->currentpage-10),$this->tagitems["previoustenpagelink"]);
   $this->tpl=str_replace($this->tagvalues["previoustenpagelink"],$t,$this->tpl);
  }else{
   $this->tpl=str_replace($this->tagvalues["previoustenpage"],$this->tagitems["previoustenpage"],$this->tpl);
  }
  if($this->currentpage+10<=$this->totalpage){
   $t=str_replace("{link}",$this->pageurl.($this->currentpage+10),$this->tagitems["nexttenpagelink"]);
   $this->tpl=str_replace($this->tagvalues["nexttenpagelink"],$t,$this->tpl);
  }else{
   $this->tpl=str_replace($this->tagvalues["nexttenpage"],$this->tagitems["nexttenpage"],$this->tpl);
  }

  //数字列表
  $firstnum;
  $lastnum;
  $t="";
  if($this->currentpage-$this->leftoffset<1){
   $firstnum=1;
  }else{$firstnum=$this->currentpage-$this->leftoffset;}
  if($this->currentpage+$this->rightoffset>$this->totalpage){
   $lastnum=$this->totalpage;
  }else{$lastnum=$this->currentpage+$this->rightoffset;}
  for($i=$firstnum;$i<=$lastnum;$i++){
   if($i==$this->currentpage){
    $t.=str_replace("{list}",$i,$this->tagitems["listpage"]);
   }else{
    $m=str_replace("{list}",$i,$this->tagitems["listpagelink"]);
    $t.=str_replace("{link}",$this->pageurl.$i,$m);
   }
  }
  $this->tpl=str_replace($this->tagvalues["listpage"],$t,$this->tpl);
  //$list=str_replace("{list}","1",$this->tagitems["listpage"]);
  //$this->tpl=str_replace($this->tagvalues["listpage"],$list,$this->tpl);

  //共{$datacount}条记录,当前为第{$currentpage}页,共{$totalpage}页,每页{$numperpage}条
  $this->tpl=str_replace("{datacount}",$this->recordcount,$this->tpl);
  $this->tpl=str_replace("{currentpage}",$this->currentpage,$this->tpl);
  $this->tpl=str_replace("{totalpage}",$this->totalpage,$this->tpl);
  $this->tpl=str_replace("{numperpage}",$this->pagesize,$this->tpl);
 }

 //输出
 function output(){
  return $this->tpl;
 }
}

?>

 

 

//检测文件demo.php




pagetpl_1

css" rel="stylesheet" type="text/css" />


require_once("page.class.php");
$conn=mysql_connect("localhost","root","root");//连接数据库教程
mysql_select_db("pht");//打开数据库

$p=new page($_get["page"],2,2,7,"?page=");//初始化
$sql="select * from comments ".$p->setlimit();//构造select * from tb limit m,n语句
$p->executesql($sql,$conn);//执行sql
while($rs=$p->recordset()){//读出记录
 echo "
".$rs["cname"]."
";
}
$sql="select count(cid) as uid from comments";//读出总记录数
$p->getrecordcount($sql,$conn);
$p->gettemplate("style.html");//获取模板内容
$p->prasetemplate();//解析模板
echo $p->output();//输出分页
?>


//分页调用模板文件




pagetpl_1





{previoustenpage}

{firstpage}

{previouspage}

{list}

{nextpage}

{lastpage}

{nexttenpage}

共{totalpage}页

每页{numperpage}条

当前为第{currentpage}页

共{datacount}条记录




热门栏目