打包(可直接运行查看效果): pagebar.rar
PHP代码
- <?php
- /*
- Author: zwws
- Date: 2007.08.16
- */
- class pagebar
- {
- // 数据总数
- public $mRsCount;
- // 分页总数
- public $mPgCount;
- // 页码显示数
- public $mItemNum;
- // 当前页
- public $mCurrent;
- // 构造函数(数据集, 每页显示记录数, 页码显示数, 当前页)
- function __construct($queryid, $unitnum, $itemnum, $current) {
- // 数据总数(实际使用)
- // $this->mRsCount = mysql_num_rows($queryid);
- // 数据总数(测试使用)
- $this->mRsCount = 500;
- $this->mPgCount = ceil($this->mRsCount / $unitnum);
- $this->mItemNum = $itemnum;
- $this->mCurrent = $current;
- }
- // 分页页码
- function itemlist() {
- // 此变量只为以下流程方便调用而设,并无实际含义
- $compare = floor($this->mItemNum / 2);
- // 计算页码
- if ($this->mPgCount <= $this->mItemNum) {
- $start = 1;
- $end = $this->mPgCount;
- } else {
- if ($this->mCurrent <= $compare) {
- $start = 1;
- $end = $this->mItemNum;
- } elseif ($this->mCurrent + $compare > $this->mPgCount) {
- $start = $this->mPgCount - $this->mItemNum + 1;
- $end = $this->mPgCount;
- } else {
- $start = $this->mCurrent - ceil($this->mItemNum / 2) + 1;
- $end = $this->mCurrent + $compare;
- }
- }
- // 生成页码
- return range($start, $end);
- }
- // 分页信息
- function iteminfo() {
- if ($this->mCurrent == 1) {
- $prev = null;
- } else {
- $prev = $this->mCurrent - 1;
- }
- if ($this->mCurrent == $this->mPgCount) {
- $next = null;
- } else {
- $next = $this->mCurrent + 1;
- }
- if ($this->mCurrent > ceil($this->mItemNum / 2)) {
- $first = 1;
- } else {
- $first = null;
- }
- if ($this->mPgCount - $this->mCurrent > floor($this->mItemNum / 2)) {
- $last = $this->mPgCount;
- } else {
- $last = null;
- }
- $iteminfo = array('first' => $first,
- 'last' => $last,
- 'prev' => $prev,
- 'next' => $next,
- 'current' => $this->mCurrent,
- 'rscount' => $this->mRsCount,
- 'pgcount' => $this->mPgCount);
- return $iteminfo;
- }
- }
- ?>


