这是一款比较经典的php分页代码,利用了程序模板,页面分离的方式来做这个文件分页功能,实在是太牛了,实例代码如下.
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));
- (emptyempty($this->currentpage))?$this->currentpage=1:$this->currentpage=$this->currentpage;
- $this->pagesize=ceil(abs(@$pagesize+0));
- (emptyempty($this->pagesize))?$this->pagesize=5:$this->pagesize=$this->pagesize;
- $this->leftoffset=ceil(abs(@$leftoffset+0));
- (emptyempty($this->leftoffset))?$this->leftoffset=2:$this->leftoffset=$this->leftoffset;
- $this->rightoffset=ceil(abs(@$rightoffset+0));
- (emptyempty($this->rightoffset))?$this->rightoffset=7:$this->rightoffset=$this->rightoffset;
- $this->pageurl=$pageurl;
-
- $this->setdefaulttagvalue();
- }
-
-
-
- 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);
- }
-
-
- 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,"<!--templatebegin-->");
- $end=strpos($this->template,"<!--templateend-->");
- $this->tpl=substr($this->template,$start+strlen("<!--templatebegin-->"),$end-$start-strlen("<!--templateend-->")-2);
- if($this->tpl==""){echo "模板内容为空,请检查标签设置是否正确。";exit();}
-
- }
-
-
- function setdefaulttagvalue(){
- $this->tagitems["previouspage"]="上一页";
- $this->tagitems["previouspagelink"]="<a href='{link}'>上一页</a>";
- $this->tagitems["previoustenpage"]="上十页";
- $this->tagitems["previoustenpagelink"]="<a href='{link}'>上十页</a>";
- $this->tagitems["nextpage"]="下一页";
- $this->tagitems["nextpagelink"]="<a href='{link}'>下一页</a>";
- $this->tagitems["nexttenpage"]="下十页";
- $this->tagitems["nexttenpagelink"]="<a href='{link}'>下十页</a>";
- $this->tagitems["firstpage"]="首页";
- $this->tagitems["firstpagelink"]="<a href='{link}'>首页</a>";
- $this->tagitems["lastpage"]="尾页";
- $this->tagitems["lastpagelink"]="<a href='{link}'>尾页</a>";
- $this->tagitems["listpage"]="[{list}]";
- $this->tagitems["listpagelink"]="<a href='{link}'>[{list}]</a>";
-
-
- $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}";
-
-
-
-
-
-
- }
-
- function settagvalue($item,$itemvalue="",$value=""){
- if(!isset($item)||!isset($itemvalue)||!isset($value)){return;}
- foreach($this->tagitems as $key=>$v){
- if($key==$item){
- (emptyempty($itemvalue))?"":$this->tagitems[$key]=$itemvalue;
- (emptyempty($value))?"":$this->tagvalues[$key]=$value;
- }
- }
- }
-
-
-
-
- function prasetemplate(){
-
- if($this->totalpage > 1){
-
- 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);
-
- 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);
- }
-
- }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);
- }
-
- }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);
- }
-
-
-
- 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);
-
-
-
-
- $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;
- }
- }
-
- ?>
-
-
- <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <title>pagetpl_1</title>
- <style type="text/css教程">
- body,div,table,tr,td {font-family:arial;font-size:14px;}
- .global {text-align:left;padding:2px;}
- .index {
- border-style:solid;
- border-width:1px;
- color:#0099cc;
- height:15px;
- text-align:center;
- float:left;
- margin:2px;
- padding:4px;
- }
- .index a:link{text-decoration:none;color:#3399ff;font-weight:bold;}
- </style>
- <link href="style.css" rel="stylesheet" type="text/css" />
- </head>
-
- <body>
- <?php
- 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();
- $p->executesql($sql,$conn);
- while($rs=$p->recordset()){
- echo "<br />".$rs["cname"]."<br />";
- }
- $sql="select count(cid) as uid from comments";
- $p->getrecordcount($sql,$conn);
- $p->gettemplate("style.html");
- $p->prasetemplate();
- echo $p->output();
- ?>
- </body>
- </html>
-
-
-
- <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <title>pagetpl_1</title>
- <style type="text/css">
- body,div,table,tr,td {font-family:arial;font-size:14px;}
- .global {text-align:left;padding:2px;}
- .index {
- border-style:solid;
- border-width:1px;
- color:#0099cc;
- height:15px;
- text-align:center;
- float:left;
- margin:2px;
- padding:4px;
- }
- .index a:link{text-decoration:none;color:#3399ff;font-weight:bold;}
- </style>
- <link href="style.css" rel="stylesheet" type="text/css" />
- </head>
-
- <body>
- <!--templatebegin-->
- <div class="global">
- <div class="index">{previoustenpage}</div>
- <div class="index">{firstpage}</div>
- <div class="index">{previouspage}</div>
- <div class="index">{list}</div>
- <div class="index">{nextpage}</div>
- <div class="index">{lastpage}</div>
- <div class="index">{nexttenpage}</div>
-
- <div class="index">共{totalpage}页</div>
- <div class="index">每页{numperpage}条</div>
- <div class="index">当前为第{currentpage}页</div>
- <div class="index">共{datacount}条记录</div>
-
- </div>
- <!--templateend-->
- </body>
- </html>
|