這是一款比較經典的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();
- }
- //取得記錄總數
- //$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,"<!--templatebegin-->");
- $end=strpos($this->template,"<!--templateend-->");
- $this->tpl=substr($this->template,$start+strlen("<!--templatebegin-->"),$end-$start-strlen("<!--templateend-->")-2);
- if($this->tpl==""){echo "模板內容為空,請檢查標簽設置是否正確。";exit();}
- //echo $this->tpl;
- }
- //設定默認標簽對應值
- 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}";
- /*其他標簽直接替換
- {$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){
- (emptyempty($itemvalue))?"":$this->tagitems[$key]=$itemvalue;//如果為空,則不改變
- (emptyempty($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
- <!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();//構造select * from tb limit m,n語句
- $p->executesql($sql,$conn);//執行sql
- 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>
- //開源代碼Vevb.com
- <div class="index">共{totalpage}頁</div>
- <div class="index">每頁{numperpage}條</div>
- <div class="index">當前為第{currentpage}頁</div>
- <div class="index">共{datacount}條記錄</div>
- </div>
- <!--templateend-->
- </body>
- </html>
|
新聞熱點
疑難解答