PHP通用分页类page.php[仿google分页]

前端之家收集整理的这篇文章主要介绍了PHP通用分页类page.php[仿google分页]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

page.PHP @H_403_0@<div class="codetitle"><a style="CURSOR: pointer" data="25423" class="copybut" id="copybut25423" onclick="doCopy('code25423')"> 代码如下:

<div class="codebody" id="code25423">@H_403_0@<?PHP / @H_403_0@ 通用PHP分页类。(仿Google样式) @H_403_0@ 只需提供记录总数与每页显示数两个参数。(已附详细使用说明..) @H_403_0@ 无需指定URL,链接由程序生成。方便用于检索结果分页。 @H_403_0@ 表单采用GET方法提交,可保证在诸如查询之,删除之类的操作时,不丢失URL参数 @H_403_0@/ class Pager{ @H_4030@//IE地址栏地址 @H4030@var $url; @H4030@//记录总条数 @H4030@var $countall; @H4030@//总页数 @H4030@var $page; @H403_0@//分页数字链接 @H_4030@var $thestr; @H403_0@//首页上一页链接 @H_4030@var $backstr; @H403_0@//尾页、下一页链接 @H_4030@var $nextstr; @H403_0@//当前页码 @H_4030@var $pg; @H403_0@//每页显示记录数量 @H_4030@var $countlist; @H403_0@//翻页样式 @H_4030@var $style; @H403_0@//构造函数,实例化该类的时候自动执行该函数 @H_4030@function Pager($countall,$countlist,$style="page"){ @H403_0@//记录数与每页显示数不能整队时,页数取余后加1 @H_4030@$this->countall = $countall; @H4030@$this->countlist = $countlist; @H4030@$this->style=$style; @H4030@if ($this->countall%$this->countlist!=0){ @H4030@$this->page=sprintf("%d",$this->countall/$this->countlist)+1; @H4030@}else{ @H4030@$this->page=$this->countall/$this->countlist; @H403_0@} $this->pg=$GET["pg"]; @H4030@//保证pg在未指定的情况下为从第1页开始 @H4030@if (!ereg("^[1-9][0-9]*$",$this->pg) || empty($this->pg)){ @H4030@$this->pg=1; @H4030@} @H403_0@//页码超出最大范围,取最大值 @H_4030@if ($this->pg>$this->page){ @H4030@$this->pg=$this->page; @H4030@} @H403_0@//得到当前的URL。具体实现请看最底部函数实体 @H_4030@$this->url = Pager::getUrl(); @H403_0@//替换错误格式的页码为正确页码 @H_403_0@if(isset($_GET["pg"]) && $GET["pg"]!=$this->pg){ @H403_0@$this->url=str_replace("?pg=".$GET["pg"],"?pg=$this->pg",$this->url); @H403_0@$this->url=str_replace("&pg=".$GET["pg"],"&pg=$this->pg",$this->url); @H4030@} @H403_0@//生成12345等数字形式的分页。 @H_4030@if ($this->page<=10){ @H4030@for ($i=1;$i<$this->page+1;$i++){ @H4030@$this->thestr=$this->thestr.Pager::makepg($i,$this->pg); @H4030@} @H4030@}else{ @H4030@if ($this->pg<=5){ @H4030@for ($i=1;$i<10;$i++){ @H4030@$this->thestr=$this->thestr.Pager::makepg($i,$this->pg); @H4030@} @H4030@}else{ @H4030@if (6+$this->pg<=$this->page){ @H4030@for ($i=$this->pg-4;$i<$this->pg+6;$i++){ @H4030@$this->thestr=$this->thestr.Pager::makepg($i,$this->pg); @H4030@} @H4030@}else{ @H4030@for ($i=$this->pg-4;$i<$this->page+1;$i++){ @H4030@$this->thestr=$this->thestr.Pager::makepg($i,$this->pg); @H4030@} } @H4030@} @H4030@} @H403_0@//生成上页下页等文字链接 @H_4030@$this->backstr = Pager::gotoback($this->pg); @H4030@$this->nextstr = Pager::gotonext($this->pg,$this->page); @H4030@//echo (" 共".$this->countall." 条,每页".$this->countlist."条,共".$this->page."页".$this->backstr.$this->thestr.$this->nextstr); @H4030@} @H403_0@//生成数字分页的辅助函数 @H_4030@function makepg($i,$pg){ @H4030@if ($i==$pg){ @H4030@return " ".$i.""; @H4030@}else{ @H4030@return " ".$i.""; @H4030@} @H4030@} @H403_0@//生成上一页等信息的函数 @H_4030@function gotoback($pg){ @H4030@if ($pg-1>0){ @H4030@return $this->gotoback=" 首页 上一页"; @H4030@}else{ @H403_0@return $this->gotoback="首页 上一页 "; @H_4030@} @H4030@} @H403_0@//生成下一页等信息的函数 @H_4030@function gotonext($pg,$page){ @H4030@if ($pg < $page){ @H4030@return " 下一页 尾页"; @H4030@}else{ @H403_0@return " 下一页 尾页"; @H_4030@} @H4030@} @H403_0@//处理url中$pg的方法,用于自动生成pg=x @H_4030@function replacepg($url,$flag,$i){ @H4030@if ($flag == 1){ @H403_0@$temppg = $this->pg; @H403_0@return str_replace("pg=".$temppg,"pg=".($this->pg+1),$url); @H4030@}else if($flag == 2) { @H403_0@$temppg = $this->pg; @H403_0@return str_replace("pg=".$temppg,"pg=".($this->pg-1),$url); @H4030@}else if($flag == 3) { @H403_0@$temppg = $this->pg; @H403_0@return str_replace("pg=".$temppg,"pg=1",$url); @H4030@}else if($flag == 4){ @H403_0@$temppg = $this->pg; @H403_0@return str_replace("pg=".$temppg,"pg=".$this->page,$url); @H4030@}else if($flag == 5){ @H403_0@$temppg = $this->pg; @H403_0@return str_replace("pg=".$temppg,"pg=".$i,$url); @H4030@}else{ @H4030@return $url; @H4030@} @H4030@} @H403_0@//获得当前URL的方法 @H_4030@function getUrl(){ @H403_0@$url="http://".$_SERVER["HTTP_HOST"]; @H_403_0@if(isset($_SERVER["REQUESTURI"])){ @H403_0@$url.=$_SERVER["REQUESTURI"]; @H4030@}else{ @H403_0@$url.=$_SERVER["PHPSELF"]; @H403_0@if(!empty($_SERVER["QUERYSTRING"])){ @H403_0@$url.="?".$_SERVER["QUERYSTRING"]; @H4030@} @H4030@} @H4030@//在当前的URL里加入pg=x字样 @H4030@if (!ereg("(pg=|PG=|pG=|Pg=)",$url)){ @H4030@if (!strpos($url,"?")){ @H4030@$url = $url."?pg=1"; @H4030@}else{ @H4030@$url = $url."&pg=1"; @H4030@} @H4030@} @H4030@return $url; @H4030@} @H4030@} @H4030@?>@H403_0@

猜你在找的PHP相关文章