首页 > 开发 > PHP > 正文

php利用模板分页程序

2024-05-04 23:06:57
字体:大 中 小
来源:转载
供稿:网友

这是一款比较经典的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.  
  237. //检测文件demo.php 
  238. <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
  239. <html xmlns="http://www.w3.org/1999/xhtml"> 
  240. <head> 
  241. <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
  242. <title>pagetpl_1</title> 
  243. <style type="text/css教程"> 
  244. body,div,table,tr,td {font-family:arial;font-size:14px;} 
  245. .global {text-align:left;padding:2px;} 
  246. .index { 
  247.     border-style:solid; 
  248.     border-width:1px; 
  249.     color:#0099cc; 
  250.     height:15px; 
  251.     text-align:center; 
  252.     float:left; 
  253.     margin:2px; 
  254.     padding:4px; 
  255. } 
  256. .index a:link{text-decoration:none;color:#3399ff;font-weight:bold;} 
  257. </style> 
  258. <link href="style.css" rel="stylesheet" type="text/css" /> 
  259. </head> 
  260.  
  261. <body> 
  262. <?php 
  263. require_once("page.class.php"); 
  264. $conn=mysql_connect("localhost","root","root");//连接数据库教程  
  265. mysql_select_db("pht");//打开数据库 
  266.  
  267. $p=new page($_get["page"],2,2,7,"?page=");//初始化 
  268. $sql="select * from comments ".$p->setlimit();//构造select * from tb limit m,n语句 
  269. $p->executesql($sql,$conn);//执行sql 
  270. while($rs=$p->recordset()){//读出记录 
  271.  echo "<br />".$rs["cname"]."<br />"; 
  272. } 
  273. $sql="select count(cid) as uid from comments";//读出总记录数 
  274. $p->getrecordcount($sql,$conn); 
  275. $p->gettemplate("style.html");//获取模板内容 
  276. $p->prasetemplate();//解析模板 
  277. echo $p->output();//输出分页 
  278. ?> 
  279. </body> 
  280. </html> 
  281.  
  282.  
  283. //分页调用模板文件 
  284. <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
  285. <html xmlns="http://www.w3.org/1999/xhtml"> 
  286. <head> 
  287. <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
  288. <title>pagetpl_1</title> 
  289. <style type="text/css"> 
  290. body,div,table,tr,td {font-family:arial;font-size:14px;} 
  291. .global {text-align:left;padding:2px;} 
  292. .index { 
  293.     border-style:solid; 
  294.     border-width:1px; 
  295.     color:#0099cc; 
  296.     height:15px; 
  297.     text-align:center; 
  298.     float:left; 
  299.     margin:2px; 
  300.     padding:4px; 
  301. } 
  302. .index a:link{text-decoration:none;color:#3399ff;font-weight:bold;} 
  303. </style> 
  304. <link href="style.css" rel="stylesheet" type="text/css" /> 
  305. </head> 
  306.  
  307. <body> 
  308. <!--templatebegin--> 
  309. <div class="global"> 
  310. <div class="index">{previoustenpage}</div> 
  311. <div class="index">{firstpage}</div> 
  312. <div class="index">{previouspage}</div> 
  313. <div class="index">{list}</div> 
  314. <div class="index">{nextpage}</div> 
  315. <div class="index">{lastpage}</div> 
  316. <div class="index">{nexttenpage}</div> 
  317. //开源代码Vevb.com 
  318. <div class="index">共{totalpage}页</div> 
  319. <div class="index">每页{numperpage}条</div> 
  320. <div class="index">当前为第{currentpage}页</div> 
  321. <div class="index">共{datacount}条记录</div> 
  322.  
  323. </div> 
  324. <!--templateend--> 
  325. </body> 
  326. </html>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表