php利用模板分页程序

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

page.class.php:

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