努力思考 + 实践, 眼高手低是不行的. 注册 | 登陆

[2007.08.16]我的分页类, 附压缩包.

 打包(可直接运行查看效果):  pagebar.rar

PHP代码
  1. <?php   
  2.   
  3. /*  
  4.   Author: zwws  
  5.   Date: 2007.08.16  
  6. */  
  7.   
  8. class pagebar   
  9. {   
  10.     // 数据总数   
  11.     public $mRsCount;   
  12.     // 分页总数   
  13.     public $mPgCount;   
  14.     // 页码显示数   
  15.     public $mItemNum;   
  16.     // 当前页   
  17.     public $mCurrent;   
  18.   
  19.     // 构造函数(数据集, 每页显示记录数, 页码显示数, 当前页)   
  20.     function __construct($queryid$unitnum$itemnum$current) {   
  21.   
  22.         // 数据总数(实际使用)   
  23.         // $this->mRsCount = mysql_num_rows($queryid);   
  24.         // 数据总数(测试使用)   
  25.         $this->mRsCount = 500;   
  26.   
  27.         $this->mPgCount = ceil($this->mRsCount / $unitnum);   
  28.         $this->mItemNum = $itemnum;   
  29.         $this->mCurrent = $current;   
  30.   
  31.     }   
  32.   
  33.     // 分页页码   
  34.     function itemlist() {   
  35.   
  36.         // 此变量只为以下流程方便调用而设,并无实际含义   
  37.         $compare = floor($this->mItemNum / 2);   
  38.   
  39.         // 计算页码   
  40.         if ($this->mPgCount <= $this->mItemNum) {   
  41.             $start = 1;   
  42.             $end   = $this->mPgCount;   
  43.         } else {   
  44.             if ($this->mCurrent <= $compare) {   
  45.                 $start = 1;   
  46.                 $end   = $this->mItemNum;   
  47.             } elseif ($this->mCurrent + $compare > $this->mPgCount) {   
  48.                 $start = $this->mPgCount - $this->mItemNum + 1;   
  49.                 $end   = $this->mPgCount;   
  50.             } else {   
  51.                 $start = $this->mCurrent - ceil($this->mItemNum / 2) + 1;   
  52.                 $end   = $this->mCurrent + $compare;   
  53.             }   
  54.         }   
  55.   
  56.         // 生成页码   
  57.         return range($start$end);   
  58.   
  59.     }   
  60.   
  61.     // 分页信息   
  62.     function iteminfo() {   
  63.   
  64.         if ($this->mCurrent == 1) {   
  65.             $prev = null;   
  66.         } else {   
  67.             $prev = $this->mCurrent - 1;   
  68.         }   
  69.         if ($this->mCurrent == $this->mPgCount) {   
  70.             $next = null;   
  71.         } else {   
  72.             $next = $this->mCurrent + 1;   
  73.         }   
  74.   
  75.         if ($this->mCurrent > ceil($this->mItemNum / 2)) {   
  76.             $first = 1;   
  77.         } else {   
  78.             $first = null;   
  79.         }   
  80.         if ($this->mPgCount - $this->mCurrent > floor($this->mItemNum / 2)) {   
  81.             $last = $this->mPgCount;   
  82.         } else {   
  83.             $last = null;   
  84.         }   
  85.   
  86.         $iteminfo = array('first'   => $first,    
  87.                            'last'    => $last,   
  88.                            'prev'    => $prev,   
  89.                            'next'    => $next,   
  90.                            'current' => $this->mCurrent,   
  91.                            'rscount' => $this->mRsCount,   
  92.                            'pgcount' => $this->mPgCount);   
  93.   
  94.         return $iteminfo;   
  95.     }   
  96. }   
  97.   
  98. ?>  

 

Tags: php原创, 分页,

« 上一篇 | 下一篇 »

Trackbacks

点击获得Trackback地址,Encode: UTF-8

本文因为某种原因此时不允许访客进行评论