- <?PHP
- class page
- {
- var $curPage; // 当前页
- var $pageBarNum; // 分页条显示页码数
- var $queryId; // 数据集
- var $rsCount; // 数据数
- var $pageCount; // 总页数
- var $rsOffset; // 查询偏移量
- var $urlSuffix; // URL后缀
- var $startNum;
- var $endNum;
- var $prevPage;
- var $nextPgae;
- // 构造函数
- // function __construct() {
- function page($dbTable, $curPage, $eveNum, $pageBarNum, $sqlSuffix = '')
- {
- global $db;
- $sqlStr = "SELECT count(*) FROM `{$dbTable}` {$sqlSuffix}";
- $this->rsCount = $db->getResult($sqlStr);
- $this->pageCount = ceil($this->rsCount / $eveNum);
- $this->pageBarNum = $pageBarNum;
- if ($curPage < 1) {
- $this->curPage = 1;
- } elseif ($curPage > $this->pageCount) {
- $this->curPage = $this->pageCount;
- } else {
- $this->curPage = $curPage;
- }
- $this->rsOffset = ($this->curPage - 1) * $eveNum;;
- $sqlStr = "SELECT *
- FROM `{$dbTable}`
- {$sqlSuffix}
- ORDER BY `id` DESC
- LIMIT {$this->rsOffset},{$eveNum}";
- $this->queryId = $db->query($sqlStr);
- $this->getNum();
- }
- // 分页条
- function getNum()
- {
- $chk1 = floor($this->pageBarNum / 2);
- $chk2 = ceil($this->pageBarNum / 2);
- if ($this->pageCount <= $this->pageBarNum) {
- $this->startNum = 1;
- $this->endNum = $this->pageCount;
- } else {
- if ($this->curPage <= $chk1) {
- $this->startNum = 1;
- $this->endNum = $this->pageBarNum;
- } elseif ($this->curPage + $chk1 > $this->pageCount) {
- $this->startNum = $this->pageCount - $this->pageBarNum + 1;
- $this->endNum = $this->pageCount;
- } else {
- $this->startNum = $this->curPage - $chk2 + 1;
- $this->endNum = $this->curPage + $chk1;
- }
- }
- if ($this->curPage == 1) {
- $this->prevPage = 1;
- } else {
- $this->prevPage = $this->curPage - 1;
- }
- if ($this->curPage == $this->pageCount) {
- $this->nextPage = $this->pageCount;
- } else {
- $this->nextPage = $this->curPage + 1;
- }/*
- $urlSuffix = explode('&', $_SERVER['QUERY_STRING']);
- foreach($urlSuffix as $k => $v) {
- $v = explode('=', $v);
- if ($v[0] == 'page') {
- unset($urlSuffix[$k]);
- }
- }
- $urlSuffix = join('&', $urlSuffix);*/
- $urlSuffix = $_GET;
- if (array_key_exists('page', $urlSuffix)) {
- unset($urlSuffix['page']);
- }
- $urlSuffix = http_build_query($urlSuffix);
- $this->urlSuffix = !emptyempty($urlSuffix) ? $urlSuffix.'&' : '';
- }
- function getBar()
- {
- $pageBar = <<<EOT
- <div id="pagelist">
- <ul>
- <li>共{$this->rsCount}条 第{$this->curPage}/{$this->pageCount}页</li>
- <li><a href="?{$this->urlSuffix}page=1" title="第一页">|<</a></li>
- <li><a href="?{$this->urlSuffix}page={$this->prevPage}" title="上一页"><<</a></li>/n
- EOT;
- for ($i = $this->startNum; $i <= $this->endNum; $i++) {
- if ($i != $this->curPage) {
- $pageBar .= " <li><a href=/"?{$this->urlSuffix}page={$i}/" title=/"第 {$i} 页/">" . $i . "</a></li>/n";
- } else {
- $pageBar .= " <li><a title=/"当前第 {$i} 页/"><b>" . $i ."</b></a></li>/n";
- }
- }
- $pageBar .= <<<EOT
- <li><a href="?{$this->urlSuffix}page={$this->nextPage}" title="下一页">>></a></li>
- <li><a href="?{$this->urlSuffix}page={$this->pageCount}" title="最后一页">>|</a></li>
- </ul>
- </div>
- EOT;
- return $pageBar;
- }
- // 数据集
- function getRS()
- {
- return $this->queryId;
- }
- // 数据总数
- function getRSC()
- {
- return $this->rsCount;
- }
- // 记录编号偏移量
- function getRSN()
- {
- return $this->rsCount - $this->rsOffset;
- }
- }
- class newpage extends page
- {
- function getBar()
- {
- $pageBar = <<<EOT
- <div id="pagelist">
- <ul>
- <li>共{$this->rsCount}条 第{$this->curPage}/{$this->pageCount}页</li>
- <li><a href="?{$this->urlSuffix}page=1" title="第一页">首</a></li>
- <li><a href="?{$this->urlSuffix}page={$this->prevPage}" title="上一页">上</a></li>/n
- EOT;
- $pageBar .= <<<EOT
- <li><a href="?{$this->urlSuffix}page={$this->nextPage}" title="下一页">下</a></li>
- <li><a href="?{$this->urlSuffix}page={$this->pageCount}" title="最后一页">末</a></li>
- </ul>
- </div>
- EOT;
- return $pageBar;
- }
- }
我的分页类
最新推荐文章于 2024-03-18 18:03:28 发布