PHP分页类

  1. <?php
  2. // 禁止直接访问该页面
  3. if (basename($HTTP_SERVER_VARS['PHP_SELF']) == "pager.class.php") {
  4.    header("HTTP/1.0 404 Not Found");
  5. }
  6. class Pager
  7. {
  8.    /** 总信息数 */
  9.    var $infoCount;
  10.    /** 总页数 */
  11.    var $pageCount;
  12.    /** 每页显示条数 */
  13.    var $items;
  14.    /** 当前页码 */
  15.    var $pageNo;
  16.    /** 查询的起始位置 */
  17.    var $startPos;
  18.    var $nextPageNo;
  19.    var $prevPageNo;
  20.  
  21.    function Pager($infoCount, $items, $pageNo)
  22.    {
  23.      $this->infoCount = $infoCount;
  24.      $this->items   = $items;
  25.      $this->pageNo  = $pageNo;
  26.      $this->pageCount = $this->GetPageCount();
  27.      $this->AdjustPageNo();
  28.      $this->startPos = $this->GetStartPos();
  29.    }
  30.    function AdjustPageNo()
  31.    {
  32.      if($this->pageNo == '' || $this->pageNo < 1)
  33.        $this->pageNo = 1;
  34.      if ($this->pageNo > $this->pageCount)
  35.        $this->pageNo = $this->pageCount;
  36.    }
  37.    /**
  38.    * 下一页
  39.    */
  40.    function GoToNextPage()
  41.    {
  42.      $nextPageNo = $this->pageNo 1;
  43.      if ($nextPageNo > $this->pageCount)
  44.      {
  45.        $this->nextPageNo = $this->pageCount;
  46.        return false;
  47.      }
  48.      $this->nextPageNo = $nextPageNo;
  49.      return true;
  50.    }
  51.    /**
  52.    * 上一页
  53.    */
  54.    function GotoPrevPage()
  55.    {
  56.      $prevPageNo = $this->pageNo - 1;
  57.      if ($prevPageNo < 1)
  58.      {
  59.        $this->prevPageNo = 1;
  60.        return false;
  61.      }
  62.      $this->prevPageNo = $prevPageNo;
  63.      return true;
  64.    }
  65.    function GetPageCount()
  66.    {
  67.      return ceil($this->infoCount / $this->items);
  68.    }
  69.    function GetStartPos()
  70.    {
  71.      return ($this->pageNo - 1) * $this->items;
  72.    }
  73. }
  74. ?>