这是一款比较经典的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)); ( empty empty ( $this ->currentpage))? $this ->currentpage=1: $this ->currentpage= $this ->currentpage; $this ->pagesize= ceil ( abs (@ $pagesize +0)); ( empty empty ( $this ->pagesize))? $this ->pagesize=5: $this ->pagesize= $this ->pagesize; $this ->leftoffset= ceil ( abs (@ $leftoffset +0)); ( empty empty ( $this ->leftoffset))? $this ->leftoffset=2: $this ->leftoffset= $this ->leftoffset; $this ->rightoffset= ceil ( abs (@ $rightoffset +0)); ( empty empty ( $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 ){ ( empty empty ( $itemvalue ))? "" : $this ->tagitems[ $key ]= $itemvalue ; //如果为空,则不改变 ( empty empty ( $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> //开源代码phpfensi.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>声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did27958