当前位置:首页 > PHP教程 > php分页 > 列表

php利用模板分页程序

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-09 13:29:34 浏览: 评论:0 

这是一款比较经典的php分页代码,利用了程序模板,页面分离的方式来做这个文件分页功能,实在是太牛了,实例代码如下.

page.class.php:

  1. class page{ 
  2.  
  3.  var $currentpage
  4.  var $leftoffset
  5.  var $rightoffset
  6.      
  7.  var $totalpage;//总页数 
  8.  var $recordcount;//总记录数 
  9.  var $pagesize;//每页显示条数 
  10.  
  11.  var $pageurl
  12.  var $hypelink
  13.  
  14.  var $template
  15.  var $tpl
  16.  var $tagitems=array(); 
  17.  var $tagvalues=array(); 
  18.  
  19.  var $sqlquery
  20.  
  21.  //构造函数 
  22.  function page($currentpage=1,$pagesize=5,$leftoffset=2,$rightoffset=7,$pageurl="?page="){ 
  23.   echo "分页类开始"
  24.   $this->currentpage=ceil(abs(@$currentpage+0)); 
  25.   (emptyempty($this->currentpage))?$this->currentpage=1:$this->currentpage=$this->currentpage; 
  26.   $this->pagesize=ceil(abs(@$pagesize+0)); 
  27.   (emptyempty($this->pagesize))?$this->pagesize=5:$this->pagesize=$this->pagesize; 
  28.   $this->leftoffset=ceil(abs(@$leftoffset+0)); 
  29.   (emptyempty($this->leftoffset))?$this->leftoffset=2:$this->leftoffset=$this->leftoffset; 
  30.   $this->rightoffset=ceil(abs(@$rightoffset+0)); 
  31.   (emptyempty($this->rightoffset))?$this->rightoffset=7:$this->rightoffset=$this->rightoffset; 
  32.   $this->pageurl=$pageurl
  33.  
  34.   $this->setdefaulttagvalue(); 
  35.  } 
  36.      
  37.  //取得记录总数 
  38.  //$sql="select count(id) as n from table"; 
  39.  function getrecordcount($sql,$conn){ 
  40.   $query=@mysql教程_query($sql,$conn); 
  41.   if(!$query){echo "执行sql语句失败";exit();} 
  42.   while($rs=mysql_fetch_row($query)){ 
  43.    $this->recordcount=$rs[0];//取得记录总数 
  44.   } 
  45.   $this->totalpage=ceil($this->recordcount / $this->pagesize);//计算总页数 
  46.   if($this->currentpage > $this->totalpage){$this->currentpage=$this->totalpage;}//判断当前页是否大于总页数 
  47.   mysql_free_result($query); 
  48.  } 
  49.      
  50.  //select * from tb p->setlimit(); 
  51.  function setlimit(){ 
  52.   $limit="limit ".($this->currentpage-1)*$this->pagesize; 
  53.   $limit.=",$this->pagesize"
  54.   return $limit
  55.  } 
  56.  
  57.  function executesql($sql,$conn){ 
  58.   if(!$sql||!$conn){echo "参数传递错误";return false;} 
  59.      $this->sqlquery=mysql_query($sql,$conn); 
  60.      if(!$this->sqlquery){echo "执行sql语句失败";return false;} 
  61.  } 
  62.  function recordset(){ 
  63.   return mysql_fetch_array($this->sqlquery); 
  64.  } 
  65.      
  66.  //取得模板内容 
  67.  function gettemplate($filedir){ 
  68.   if(file_exists($filedir)){ 
  69.    $f=fopen($filedir,"r"); 
  70.    $this->template=fread($f,filesize($filedir)); 
  71.   }else
  72.    echo "获取模板文件失败...文件不存在"
  73.    exit(); 
  74.   } 
  75.   //取得区块内容 
  76.   $start=strpos($this->template,"<!--templatebegin-->"); 
  77.   $end=strpos($this->template,"<!--templateend-->"); 
  78.   $this->tpl=substr($this->template,$start+strlen("<!--templatebegin-->"),$end-$start-strlen("<!--templateend-->")-2); 
  79.   if($this->tpl==""){echo "模板内容为空,请检查标签设置是否正确。";exit();} 
  80.   //echo $this->tpl; 
  81.  } 
  82.  
  83.  //设定默认标签对应值 
  84.  function setdefaulttagvalue(){ 
  85.   $this->tagitems["previouspage"]="上一页"
  86.   $this->tagitems["previouspagelink"]="<a href='{link}'>上一页</a>"
  87.   $this->tagitems["previoustenpage"]="上十页"
  88.   $this->tagitems["previoustenpagelink"]="<a href='{link}'>上十页</a>"
  89.   $this->tagitems["nextpage"]="下一页"
  90.   $this->tagitems["nextpagelink"]="<a href='{link}'>下一页</a>"
  91.   $this->tagitems["nexttenpage"]="下十页"
  92.   $this->tagitems["nexttenpagelink"]="<a href='{link}'>下十页</a>"
  93.   $this->tagitems["firstpage"]="首页"
  94.   $this->tagitems["firstpagelink"]="<a href='{link}'>首页</a>"
  95.   $this->tagitems["lastpage"]="尾页"
  96.   $this->tagitems["lastpagelink"]="<a href='{link}'>尾页</a>"
  97.   $this->tagitems["listpage"]="[{list}]"
  98.   $this->tagitems["listpagelink"]="<a href='{link}'>[{list}]</a>"
  99.          
  100.   //定义模板标签 
  101.   $this->tagvalues["previouspage"]="{previouspage}"
  102.   $this->tagvalues["previouspagelink"]="{previouspage}"
  103.   $this->tagvalues["previoustenpage"]="{previoustenpage}"
  104.   $this->tagvalues["previoustenpagelink"]="{previoustenpage}"
  105.   $this->tagvalues["nextpage"]="{nextpage}"
  106.   $this->tagvalues["nextpagelink"]="{nextpage}"
  107.   $this->tagvalues["nexttenpage"]="{nexttenpage}"
  108.   $this->tagvalues["nexttenpagelink"]="{nexttenpage}"
  109.   $this->tagvalues["firstpage"]="{firstpage}"
  110.   $this->tagvalues["firstpagelink"]="{firstpage}"
  111.   $this->tagvalues["lastpage"]="{lastpage}"
  112.   $this->tagvalues["lastpagelink"]="{lastpage}"
  113.   $this->tagvalues["listpage"]="{list}"
  114.   $this->tagvalues["listpagelink"]="{list}"
  115.   /*其他标签直接替换 
  116.   {$datacount}:共{$datacount}条记录 
  117.   {$currentpage}:当前为第{$currentpage}页 
  118.   {$totalpage}:共{$totalpage}页 
  119.   {$numperpage}:每页{$numperpage}条 
  120.   */ 
  121.  } 
  122.  // 重新设定标签对应值 
  123.  function settagvalue($item,$itemvalue="",$value=""){ 
  124.   if(!isset($item)||!isset($itemvalue)||!isset($value)){return;} 
  125.   foreach($this->tagitems as $key=>$v){ 
  126.    if($key==$item){ 
  127.     (emptyempty($itemvalue))?"":$this->tagitems[$key]=$itemvalue;//如果为空,则不改变 
  128.     (emptyempty($value))?"":$this->tagvalues[$key]=$value
  129.    } 
  130.   } 
  131.  } 
  132.  
  133.  
  134.  
  135.  //模板解析 
  136.  function prasetemplate(){ 
  137.   //------a_begin------// 
  138.   if($this->totalpage > 1){ 
  139.    //------b_begin------// 
  140.    if($this->currentpage > 1){ 
  141.     //首页 
  142.     $t=str_replace("{link}",$this->pageurl."1",$this->tagitems["firstpagelink"]); 
  143.     $this->tpl=str_replace($this->tagvalues["firstpagelink"],$t,$this->tpl); 
  144.     //前一页 
  145.     $t=str_replace("{link}",$this->pageurl.($this->currentpage-1),$this->tagitems["previouspagelink"]); 
  146.     $this->tpl=str_replace($this->tagvalues["previouspagelink"],$t,$this->tpl); 
  147.     //------c_begin------// 
  148.     if($this->currentpage < $this->totalpage){ 
  149.      //下一页 
  150.      $t=str_replace("{link}",$this->pageurl.($this->currentpage+1),$this->tagitems["nextpagelink"]); 
  151.      $this->tpl=str_replace($this->tagvalues["nextpagelink"],$t,$this->tpl); 
  152.      //尾页 
  153.      $t=str_replace("{link}",$this->pageurl.$this->totalpage,$this->tagitems["lastpagelink"]); 
  154.      $this->tpl=str_replace($this->tagvalues["lastpagelink"],$t,$this->tpl); 
  155.     }else
  156.      //下一页 
  157.      $this->tpl=str_replace($this->tagvalues["nextpage"],$this->tagitems["nextpage"],$this->tpl); 
  158.      //尾页 
  159.      $this->tpl=str_replace($this->tagvalues["lastpage"],$this->tagitems["lastpage"],$this->tpl); 
  160.     } 
  161.     //------c_end------// 
  162.    }else
  163.     //首页 
  164.     $this->tpl=str_replace($this->tagvalues["firstpage"],$this->tagitems["firstpage"],$this->tpl); 
  165.     //前一页 
  166.     $this->tpl=str_replace($this->tagvalues["previouspage"],$this->tagitems["previouspage"],$this->tpl); 
  167.     //下一页 
  168.     $t=str_replace("{link}",$this->pageurl.($this->currentpage+1),$this->tagitems["nextpagelink"]); 
  169.     $this->tpl=str_replace($this->tagvalues["nextpagelink"],$t,$this->tpl); 
  170.     //尾页 
  171.     $t=str_replace("{link}",$this->pageurl.$this->totalpage,$this->tagitems["lastpagelink"]); 
  172.     $this->tpl=str_replace($this->tagvalues["lastpagelink"],$t,$this->tpl); 
  173.    } 
  174.    //------b_end------// 
  175.   }else
  176.    //解析前一页,前十页,后一页,后十页,首页,尾页 
  177.    $this->tpl=str_replace($this->tagvalues["previouspage"],$this->tagitems["previouspage"],$this->tpl); 
  178.    $this->tpl=str_replace($this->tagvalues["previoustenpage"],$this->tagitems["previoustenpage"],$this->tpl); 
  179.    $this->tpl=str_replace($this->tagvalues["nextpage"],$this->tagitems["nextpage"],$this->tpl); 
  180.    $this->tpl=str_replace($this->tagvalues["nexttenpage"],$this->tagitems["nexttenpage"],$this->tpl); 
  181.    $this->tpl=str_replace($this->tagvalues["firstpage"],$this->tagitems["firstpage"],$this->tpl); 
  182.    $this->tpl=str_replace($this->tagvalues["lastpage"],$this->tagitems["lastpage"],$this->tpl); 
  183.   } 
  184.   //------a_end------// 
  185.  
  186.   //前十页,后十页 
  187.   if($this->currentpage-10>=1){ 
  188.    $t=str_replace("{link}",$this->pageurl.($this->currentpage-10),$this->tagitems["previoustenpagelink"]); 
  189.    $this->tpl=str_replace($this->tagvalues["previoustenpagelink"],$t,$this->tpl); 
  190.   }else
  191.    $this->tpl=str_replace($this->tagvalues["previoustenpage"],$this->tagitems["previoustenpage"],$this->tpl); 
  192.   } 
  193.   if($this->currentpage+10<=$this->totalpage){ 
  194.    $t=str_replace("{link}",$this->pageurl.($this->currentpage+10),$this->tagitems["nexttenpagelink"]); 
  195.    $this->tpl=str_replace($this->tagvalues["nexttenpagelink"],$t,$this->tpl); 
  196.   }else
  197.    $this->tpl=str_replace($this->tagvalues["nexttenpage"],$this->tagitems["nexttenpage"],$this->tpl); 
  198.   } 
  199.  
  200.   //数字列表 
  201.   $firstnum
  202.   $lastnum
  203.   $t=""
  204.   if($this->currentpage-$this->leftoffset<1){ 
  205.    $firstnum=1; 
  206.   }else{$firstnum=$this->currentpage-$this->leftoffset;} 
  207.   if($this->currentpage+$this->rightoffset>$this->totalpage){ 
  208.    $lastnum=$this->totalpage; 
  209.   }else{$lastnum=$this->currentpage+$this->rightoffset;} 
  210.   for($i=$firstnum;$i<=$lastnum;$i++){ 
  211.    if($i==$this->currentpage){ 
  212.     $t.=str_replace("{list}",$i,$this->tagitems["listpage"]); 
  213.    }else
  214.     $m=str_replace("{list}",$i,$this->tagitems["listpagelink"]); 
  215.     $t.=str_replace("{link}",$this->pageurl.$i,$m); 
  216.    } 
  217.   } 
  218.   $this->tpl=str_replace($this->tagvalues["listpage"],$t,$this->tpl); 
  219.   //$list=str_replace("{list}","1",$this->tagitems["listpage"]); 
  220.   //$this->tpl=str_replace($this->tagvalues["listpage"],$list,$this->tpl); 
  221.  
  222.   //共{$datacount}条记录,当前为第{$currentpage}页,共{$totalpage}页,每页{$numperpage}条 
  223.   $this->tpl=str_replace("{datacount}",$this->recordcount,$this->tpl); 
  224.   $this->tpl=str_replace("{currentpage}",$this->currentpage,$this->tpl); 
  225.   $this->tpl=str_replace("{totalpage}",$this->totalpage,$this->tpl); 
  226.   $this->tpl=str_replace("{numperpage}",$this->pagesize,$this->tpl); 
  227.  } 
  228.  
  229.  //输出 
  230.  function output(){ 
  231.   return $this->tpl; 
  232.  } 
  233.  
  234. ?> 
  235.  
  236. //检测文件demo.php 
  237. <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"
  238. <html xmlns="http://www.w3.org/1999/xhtml"
  239. <head> 
  240. <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
  241. <title>pagetpl_1</title> 
  242. <style type="text/css教程"
  243. body,div,table,tr,td {font-family:arial;font-size:14px;} 
  244. .global {text-align:left;padding:2px;} 
  245. .index { 
  246.     border-style:solid; 
  247.     border-width:1px; 
  248.     color:#0099cc; 
  249.     height:15px; 
  250.     text-align:center; 
  251.     float:left; 
  252.     margin:2px; 
  253.     padding:4px; 
  254. .index a:link{text-decoration:none;color:#3399ff;font-weight:bold;} 
  255. </style> 
  256. <link href="style.css" rel="stylesheet" type="text/css" /> 
  257. </head> 
  258.  
  259. <body> 
  260. <?php 
  261. require_once("page.class.php"); 
  262. $conn=mysql_connect("localhost","root","root");//连接数据库教程  
  263. mysql_select_db("pht");//打开数据库 
  264.  
  265. $p=new page($_get["page"],2,2,7,"?page=");//初始化 
  266. $sql="select * from comments ".$p->setlimit();//构造select * from tb limit m,n语句 
  267. $p->executesql($sql,$conn);//执行sql 
  268. while($rs=$p->recordset()){//读出记录 
  269.  echo "<br />".$rs["cname"]."<br />"
  270. $sql="select count(cid) as uid from comments";//读出总记录数 
  271. $p->getrecordcount($sql,$conn); 
  272. $p->gettemplate("style.html");//获取模板内容 
  273. $p->prasetemplate();//解析模板 
  274. echo $p->output();//输出分页 
  275. ?> 
  276. </body> 
  277. </html> 
  278.  
  279.  
  280. //分页调用模板文件 
  281. <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"
  282. <html xmlns="http://www.w3.org/1999/xhtml"
  283. <head> 
  284. <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
  285. <title>pagetpl_1</title> 
  286. <style type="text/css"
  287. body,div,table,tr,td {font-family:arial;font-size:14px;} 
  288. .global {text-align:left;padding:2px;} 
  289. .index { 
  290.     border-style:solid; 
  291.     border-width:1px; 
  292.     color:#0099cc; 
  293.     height:15px; 
  294.     text-align:center; 
  295.     float:left; 
  296.     margin:2px; 
  297.     padding:4px; 
  298. .index a:link{text-decoration:none;color:#3399ff;font-weight:bold;} 
  299. </style> 
  300. <link href="style.css" rel="stylesheet" type="text/css" /> 
  301. </head> 
  302.  
  303. <body> 
  304. <!--templatebegin--> 
  305. <div class="global"
  306. <div class="index">{previoustenpage}</div> 
  307. <div class="index">{firstpage}</div> 
  308. <div class="index">{previouspage}</div> 
  309. <div class="index">{list}</div> 
  310. <div class="index">{nextpage}</div> 
  311. <div class="index">{lastpage}</div> 
  312. <div class="index">{nexttenpage}</div> 
  313. //开源代码phpfensi.com 
  314. <div class="index">共{totalpage}页</div> 
  315. <div class="index">每页{numperpage}条</div> 
  316. <div class="index">当前为第{currentpage}页</div> 
  317. <div class="index">共{datacount}条记录</div> 
  318.  
  319. </div> 
  320. <!--templateend--> 
  321. </body> 
  322. </html>

Tags: php模板分页 php分页程序

分享到:

相关文章