教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 带多种分页方式的php分页类

带多种分页方式的php分页类

发布时间:2015-10-17   编辑:jiaochengji.com
一个很好用的php分页类,如果你正在寻找php的分页方法或类,那么你建议你参考下本文介绍的这段代码,它提供了多种分页显示方式,非常值得学习与参考。

1、php分页类完整代码。
 

复制代码 代码示例:

<?php
Class PAGE {
//类开始
   /********************************************************
* $total       记录总数
* $pageNum     每页显示的条数
* $url = ''    链接
* $page->StartPage(显示分类统计,字符分类/数字分页,跳转);
* $page->StartPage(true/false, true/false, true/false);
* site http://www.jbxue.com
*********************************************************/
private $total;   //记录总数
private $pageNum; //每页显示数
private $page;    //当前页数
private $pages;   //总的页数
private $url;     //页面url
private $Aque;    //URL参数
 
/* 构造函数 */
public function PAGE($total, $pageNum, $url='?'){ 
   $this->total = $total;                 //总记录数.
   $this->pageNum = $pageNum;             //每页显示数.
   $this->url = $this->StrSift($url);     //判断$url的值是否合法.
   $this->Aque = $_GET;                   //页面原来所传递参数.
   $this->page = $this->StrSift($_GET['page']);             //当前页面GET(全局变量)方式参数,当前页码. 
   $this->page = is_numeric($this->page) ? $this->page : 1; //当前页码不为数字时,则把其设为1.
   $this->pages = ceil($total/$pageNum);                    //总页数.
   if($this->page<1) $this->page = 1;                       //当页码小于1时,则把其设为1.
   //if($this->page>$this->pages) $this->page = $this->pages; //当页码大于最大页码时,则把其设为最大页码.
}
/******************
* 分页方法
*******************/
function StartPage($str, $view=true, $jump=true){
   if($view==true) $PageStr .= $this->GetCount();    //分页统计信息 
   if($str=='str') $PageStr .= $this->GetPageStr(); //选择字符分页形式
   else $PageStr .= $this->GetPageNum();        //选择数字分页形式
   if($jump==true) $PageStr .= $this->JumpSelect(); //跳转
   return $PageStr;
}

/**********************************************
* 显示统计信息. 格式:共5条记录 页:2/3
***********************************************/
function GetCount(){    
   $CountStr = "共<span>". $this->total ."</span>条记录&nbsp;页:".$this->page."/".$this->pages."&nbsp;&nbsp;";
   return $CountStr;
}

/***********************************************
* 分页格式形一:第一页 上一页 下一页 末 页
************************************************/
function GetPageStr(){
   $url = $this->url; //获取URL
   //对URL参数进行处理:数组的键是URL变量,数组的值是URL变量的值.
   foreach($this->Aque as $key => $val){
      switch($key){
     case "page":
       $Next = $val + 1;
          $Prev = $val - 1;
       break;
     default:
       $Sque .= "&$key=".$this->StrSift($val);
    }
   }
   if($Next==0) $Next=2;
   //首 页    上一页
   switch($this->page){
    case $this->page<=1:
        $pagestr .= "首 页&nbsp;&nbsp;";
      $pagestr .= "上一页&nbsp;&nbsp;";
         break;
    default:
      $pagestr .= "<a href='".$url."?page=1$Sque'>首 页</a>&nbsp;&nbsp;";
      $pagestr .= "<a href='".$url."?page=$Prev$Sque'>上一页</a>&nbsp;&nbsp;";    
   }
   //下一页    末 页
   switch($this->page){
    case $this->page>=$this->pages:
        $pagestr .= "下一页&nbsp;&nbsp;";
      $pagestr .= "末 页&nbsp;&nbsp;";
         break;
    default:
      $pagestr .= "<a href='".$url."?page=$Next$Sque'>下一页</a>&nbsp;&nbsp;";
      $pagestr .= "<a href='".$url."?page=$this->pages$Sque'>末 页</a>&nbsp;&nbsp;";
   } 
   //返回分页字符串.
   return $pagestr;
    }

/***********************************************************
* 分页格式形如:共4307条记录 页:1/72   1 2 3 4 5 6 7 8 9 10
************************************************************/
function GetPageNum(){
   $url = $this->url; 
   //对URL参数进行处理:数组的键是URL变量,数组的值是URL变量的值.
   foreach($this->Aque as $key => $val){
    switch($key){
     case $key!="page":
     $Sque .= "&$key=".$this->StrSift($val);
    }
   }
   switch($this->pages){
   //总页数大于12页:
   case $this->pages>12:
     //分页数字前:< <<
     switch($this->page){
     case $this->page>1:
     $pagestr .= "<a href='$url?page=1".$Sque."'><</a>&nbsp;";
     $pagestr .= "<a href='$url?page=".($this->page-1).$Sque."'><<</a>&nbsp;";
     break;
     default:
     $pagestr .= "<&nbsp;";
     $pagestr .= "<<&nbsp;";      
     }
     //分页数字:1 2 3 4 5 6     当前页码左边6个分页链接,右边6个分页链接.
     for($i=$this->page-6; $i<=$this->page+6; $i++){
      if($i>$this->pages) break;
      if($i==$this->page) $pagestr .= $i."&nbsp;";
      elseif($i>=1) $pagestr .= "<a href='$url?page=$i".$VarFields."'>$i</a>&nbsp;";
    }
     //分页数字后: > >>
     switch($this->page){    
     case $this->page<$this->pages:
      $pagestr .= "<a href='$url?page=".($this->page+1).$Sque."'>>></a>&nbsp;";
      $pagestr .= "<a href='$url?page=".$this->pages.$Sque."'>></a>&nbsp;";
      break;
     default:
      $pagestr .= ">&nbsp;";
      $pagestr .= ">>&nbsp;";
     }
     break;
   default:
      //总页数小于12页:
    for($i=1; $i<=$this->pages; $i++){
      switch($i){
      case $i==$this->page:
      $pagestr .= $i."&nbsp;";
      break;
      default:
      $pagestr .= "<a href='$url?page=$i".$Sque."'>$i</a>&nbsp;";
      }
    }
   }
   //返回分页字符串.
   return $pagestr;
}
 
/************************
* 定义跳转页. BEGIN
*************************/
function JumpSelect(){
   $url = $this->url; 
   //对URL参数进行处理:数组的键是URL变量,数组的值是URL变量的值.
   foreach($this->Aque as $key => $val){
      if($key != "page") $Sque .= "&$key=".$this->StrSift($val);
   }
   $SelectStr = "\n<select id='JumpSelect' name='NowPage' size='1' ";
   $SelectStr .= "onChange=\"window.location='".$url."?page='+this.value+'".$Sque."'\">\n";
          for($i=1; $i<=$this->pages; $i++){
        if($i == $this->page) $selected = " selected";
     else $selected = "";
        $SelectStr .= "<option value=$i".$selected.">$i</option>\n";
    }   
        $SelectStr .= "</select>\n";
   //返回分页字符串.
   return $SelectStr;
}

/**********************************
* 过滤特殊字符.
***********************************/
private function StrSift($str){
      $str = str_replace("\"","",$str);
   $str = str_replace("'","",$str);
   $str = str_replace("[url=file://%22,%22%22,$str/]\\","",$str[/url]);
   $str = str_replace("\/","",$str);
   $str = str_replace(":","",$str);
   $str = str_replace("?","",$str); //去除会出现"??".
   $str = str_replace(">","",$str);
   $str = str_replace("<","",$str);
   $str = str_replace("%","",$str);
   $str = str_replace("*","",$str);
   $str = str_replace("&","",$str);
   $str = str_replace(".","",$str);
   return $str;
}

//类结束
}
?>


有兴趣的朋友,还可以参考下如下的文章:
一个不错的php分页类的代码
一个实用的php分页类

分页样式表:
 

复制代码 代码示例:
<style type="text/css">
/*下拉菜单或文本输入框样式*/
#JumpSelect{
background:#159bd0;
border:1px solid #ffff66; 
color:#ffff66;
width:37px;
font-size:11px;
padding:0 0 0 0;
margin:0 0 0 0;
SCROLLBAR-FACE-COLOR: #d0dff7;
SCROLLBAR-HIGHLIGHT-COLOR: #d0dff7;
SCROLLBAR-SHADOW-COLOR: #FFFFFF;
SCROLLBAR-3DLIGHT-COLOR: #FFFFFF;
SCROLLBAR-ARROW-COLOR: #ffffff;
SCROLLBAR-TRACK-COLOR: #ffffff;
SCROLLBAR-DARKSHADOW-COLOR: #d0dff7;
base-color: #ededed;
    scrollbar-arrow-color: #000;
    scrollbar-highlight-color: #fafafa;
    scrollbar-shadow-color: #fafafa;
    scrollbar-track-color: #ededed;
    scrollbar-face-color: #fafafa;
    scrollbar-darkshadow-color: #ededed;
    scrollbar-3dlight-color: #ededed;
}
</style>

您可能感兴趣的文章:
入门级PHP简单分页代码
解决thinkphp使用paginate分页时的问题
php分页代码实例有注释
再出江湖,Jpage分页php版发布
php页面跳转函数 页面重定向
带多种分页方式的php分页类
php生成翻页链接列表的函数举例
带滑动条的jQuery分页插件 jPaginator
WordPress常用的函数、方法汇总
php提示”Cannot send session cache limiter

关键词: php分页  分页代码   
[关闭]
~ ~