<?php    
/*   
 * PHP分页类   
 * @package Page   
 * @Created 2013-03-27   
 * @Modify  2013-03-27   
 * @link http://www.60ie.net   
 * Example:   
       $myPage=new Pager(1300,intval($CurrentPage));   
       $pageStr= $myPage->GetPagerContent();   
       echo $pageStr;   
 */   
class Pager {    
    private $pageSize = 10;    
    private $pageIndex;    
    private $totalNum;

private $totalPagesCount;

private $pageUrl;    
    private static $_instance;

public function __construct($p_totalNum, $p_pageIndex, $p_pageSize = 10,$p_initNum=3,$p_initMaxNum=5) {    
        if (! isset ( $p_totalNum ) || !isset($p_pageIndex)) {    
            die ( "pager initial error" );    
        }

$this->totalNum = $p_totalNum;    
        $this->pageIndex = $p_pageIndex;    
        $this->pageSize = $p_pageSize;    
        $this->initNum=$p_initNum;    
        $this->initMaxNum=$p_initMaxNum;    
        $this->totalPagesCount= ceil($p_totalNum / $p_pageSize);    
        $this->pageUrl=$this->_getPageUrl();

$this->_initPagerLegal();    
    }

/**   
    * 获取去除page部分的当前URL字符串   
    *   
    * @return String URL字符串   
    */   
  private function _getPageUrl() {    
        $CurrentUrl = $_SERVER["REQUEST_URI"];    
        $arrUrl     = parse_url($CurrentUrl);    
        $urlQuery   = $arrUrl["query"];

if($urlQuery){    
            $urlQuery  = ereg_replace("(^|&)page=" . $this->pageIndex, "", $urlQuery);    
            $CurrentUrl = str_replace($arrUrl["query"], $urlQuery, $CurrentUrl);

if($urlQuery){    
                 $CurrentUrl.="&page";    
            }    
            else $CurrentUrl.="page";

} else {    
            $CurrentUrl.="?page";    
        }

return $CurrentUrl;

}    
  /*   
   *设置页面参数合法性   
   *@return void   
  */   
  private function _initPagerLegal()    
  {    
      if((!is_numeric($this->pageIndex)) ||  $this->pageIndex<1)    
      {    
          $this->pageIndex=1;    
      }elseif($this->pageIndex > $this->totalPagesCount)    
      {    
          $this->pageIndex=$this->totalPagesCount;    
      }

}    
//$this->pageUrl}={$i}    
//{$this->CurrentUrl}={$this->TotalPages}    
    public function GetPagerContent() {    
        $str = "<div class=\"Pagination\">";    
        //首页 上一页    
        if($this->pageIndex==1)    
        {    
            $str .="<a href='javascript:void(0)' class='tips' title='首页'>首页</a> "."\n";    
            $str .="<a href='javascript:void(0)' class='tips' title='上一页'>上一页</a> "."\n"."\n";    
        }else   
        {    
            $str .="<a href='{$this->pageUrl}=1' class='tips' title='首页'>首页</a> "."\n";    
                    $str .="<a href='{$this->pageUrl}=".($this->pageIndex-1)."' class='tips' title='上一页'>上一页</a> "."\n"."\n";    
        }

/*

除首末后 页面分页逻辑

*/   
         //10页(含)以下    
         $currnt="";    
         if($this->totalPagesCount<=10)    
         {

for($i=1;$i<=$this->totalPagesCount;$i++)

{    
                       if($i==$this->pageIndex)    
                       {    $currnt=" class='current'";}    
                       else   
                       {    $currnt="";    }    
                        $str .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;    
            }    
         }else                                //10页以上    
         {   if($this->pageIndex<3)  //当前页小于3    
             {    
                     for($i=1;$i<=3;$i++)    
                     {    
                         if($i==$this->pageIndex)    
                           {    $currnt=" class='current'";}    
                         else   
                         {    $currnt="";    }    
                        $str .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;    
                     }

$str.="<span class=\"dot\">……</span>"."\n";

for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)//功能1    
                 {    
                      $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;

}    
             }elseif($this->pageIndex<=5)   //   5 >= 当前页 >= 3    
             {    
                 for($i=1;$i<=($this->pageIndex+1);$i++)    
                 {    
                      if($i==$this->pageIndex)    
                       {    $currnt=" class='current'";}    
                       else   
                       {    $currnt="";    }    
                        $str .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;

}    
                 $str.="<span class=\"dot\">……</span>"."\n";

for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)//功能1    
                 {    
                      $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;

}

}elseif(5<$this->pageIndex  &&  $this->pageIndex<=$this->totalPagesCount-5 )             //当前页大于5,同时小于总页数-5

{

for($i=1;$i<=3;$i++)    
                 {    
                     $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;    
                 }    
                  $str.="<span class=\"dot\">……</span>";                 
                 for($i=$this->pageIndex-1 ;$i<=$this->pageIndex+1 && $i<=$this->totalPagesCount-5+1;$i++)    
                 {    
                       if($i==$this->pageIndex)    
                       {    $currnt=" class='current'";}    
                       else   
                       {    $currnt="";    }    
                        $str .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;    
                 }    
                 $str.="<span class=\"dot\">……</span>";

for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)    
                 {    
                      $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;

}    
             }else   
             {

for($i=1;$i<=3;$i++)    
                 {    
                     $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;    
                 }    
                  $str.="<span class=\"dot\">……</span>"."\n";

for($i=$this->totalPagesCount-5;$i<=$this->totalPagesCount;$i++)//功能1    
                 {    
                       if($i==$this->pageIndex)    
                       {    $currnt=" class='current'";}    
                       else   
                       {    $currnt="";    }    
                        $str .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;

}    
            }

}

/*

除首末后 页面分页逻辑结束

*/

//下一页 末页    
        if($this->pageIndex==$this->totalPagesCount)    
        {       
            $str .="\n"."<a href='javascript:void(0)' class='tips' title='下一页'>下一页</a>"."\n" ;    
            $str .="<a href='javascript:void(0)' class='tips' title='末页'>末页</a>"."\n";

}else   
        {    
            $str .="\n"."<a href='{$this->pageUrl}=".($this->pageIndex+1)."' class='tips' title='下一页'>下一页</a> "."\n";    
            $str .="<a href='{$this->pageUrl}={$this->totalPagesCount}' class='tips' title='末页'>末页</a> "."\n" ;    
        }

$str .= "</div>";    
        return $str;    
    }

/**   
 * 获得实例   
 * @return     
 */   
//  static public function getInstance() {    
//      if (is_null ( self::$_instance )) {    
//          self::$_instance = new pager ();    
//      }    
//      return self::$_instance;    
//  }

}    
?>

----调用分页类

<head>    
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
<title>----分页演示-----</title>    
<link href="pager.css" type="text/css" rel="stylesheet" />    
</head>    
<body>    
    <?php    
     include "pager.class.php";    
     $CurrentPage=isset($_GET['page'])?$_GET['page']:1;    
     //die($CurrentPage);    
     $myPage=new pager(1300,intval($CurrentPage));    
      $pageStr= $myPage->GetPagerContent();    
     //echo $pageStr;    
     $myPage=new pager(90,intval($CurrentPage));     
     $pageStr= $myPage->GetPagerContent();    
     echo $pageStr;    
    ?>    
</body>    
</html>

php漂亮的分页类的更多相关文章

  1. PHP简单漂亮的分页类

    本文介绍一款原生的PHP分页类,分页样式有点类似bootstrap. <?php /* * ********************************************* * @类名 ...

  2. THinkPHP简单漂亮的分页类 DownLoad

    PHP include_once("config.php"); require_once('page.class.php'); //分页类 $showrow = 10; //一页显 ...

  3. php分页类代码带分页样式效果(转)

    php分页类代码,有漂亮的分页样式风格 时间:2016-03-16 09:16:03来源:网络 导读:不错的php分页类代码,将类文件与分页样式嵌入,实现php查询结果的精美分页,对研究php分页原理 ...

  4. php大力力 [019节]php分页类的学习

    2015-08-26 php大力力019.php分页类的学习 [2014]兄弟连高洛峰 PHP教程14.2.1 分页需求分析 14:18 [2014]兄弟连高洛峰 PHP教程14.2.2 分页类中分页 ...

  5. 二十八、CI框架之自己写分页类,符合CI的路径规范

    一.参照了CSDN上某个前辈写的一个CI分页类,自己删删改改仿写了一个类似的分页类,代码如下: 二.我们在模型里面写2个数据查询的函数,一个用于查询数据数量,一个用于查询出具体数据 三.我们在控制器里 ...

  6. php实现的分页类

    php分页类文件: <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 pr ...

  7. asp.net的快捷实用分页类

    KeleyiPager分页类,可以于对列表页进行分页浏览,代码是从HoverTreeCMS项目中COPY的,感觉很不错,使用简单方便,但是功能强大. 在线体验效果:http://cms.hovertr ...

  8. php分页类

    1.需求 学会php分页类的使用 2.参考例子 CI的分页类 3.代码部分 <?php class pagination{ public $pagesize=20; public $pagein ...

  9. PHPCMS V9 分页类的修改教程

    首先,打开 phpcms\libs\functions\global.func.php 这个文件,找到文件第622行的分页函数,复制一下,粘贴到默认分页函数的下面,重新命名后保存.(笔者在此命名为:p ...

随机推荐

  1. tcp - 传输控制协议 (TCP)

    总缆 SYNOPSIS #include <sys/socket.h> #include <netinet/in.h> tcp_socket = socket(PF_INET, ...

  2. Django过滤器之safe

    safe除了将html生成安全的文本外,还有将整型转为字符串: {% for user in user_list %} {% if user.id|safe == uid %} <li clas ...

  3. hibernate调用oracle存储过程||函数

    pakeage dao.Impl; //调用函数FUN_GET(); public String get(String Id,String Name){ return getSession().cre ...

  4. spring boot 四大组件之Auto Configuration

    SpringBoot 自动配置主要通过 @EnableAutoConfiguration, @Conditional, @EnableConfigurationProperties 或者 @Confi ...

  5. 超实用的HTML代码段(赵荣娇)

    第1章 创建HTML文档 11.1 HTML文档的基本结构 2 <html> <head> <title>Title of page</title> & ...

  6. webapp兼容问题解决

    1. IOS移动端click事件300ms的延迟响应 移动设备上的web网页是有300ms延迟的,玩玩会造成按钮点击延迟甚至是点击失效.这是由于区分单击事件和双击屏幕缩放的历史原因造成的, 2007年 ...

  7. c# 转16进制

    1.byte[] 转换16进制字符串 1.1 BitConverter方式 var str = DateTime.Now.ToString(); var encode = Encoding.UTF8; ...

  8. Dart编程实例 - HelloWorld

    Dart编程实例 - HelloWorld void main() { print('hello world'); } 本文转自:http://codingdict.com/article/23399

  9. java中设置http响应头控制浏览器禁止缓存当前文档内容

    response.setDateHeader("expries", -1); response.setHeader("Cache-Control", " ...

  10. php开发面试题---php高级程序员需要掌握的一些知识

    php开发面试题---php高级程序员需要掌握的一些知识 一.总结 一句话总结: 还是需要多多接触架构师的知识,比如这里说的微服务,还有需要php服务端的知识来解决web端的不足,比如Swoole 1 ...