mCnt = $pCnt; if($this->mPageMax = ceil($pCnt / $this->mPagesize = $pPagesize)){ if(isset($_GET['p']) && ($this->mPage = abs($_GET['p'])) && ($this->mPageMax < $this->mPage)){ $this->mPage = $this->mPageMax; } } $this->mPage || $this->mPage = 1; } /** * 分页 */ function limit(){ return (($this->mPage - 1) * $this->mPagesize).','.$this->mPagesize; } /** * 处理连接 */ private $_href = ''; function _default_href($pHref=''){ if($pHref) return $this->_href = $pHref; if(!$this->_href){ $tGet = $_GET; if(isset($tGet['p'])) unset($tGet['p']); $tUrl = isset($_SERVER['REDIRECT_URL'])? $_SERVER['REDIRECT_URL']: ''; $tHref = $tUrl.(empty($tGet)? '?p=': '?'.http_build_query($tGet).'&p='); $this->_href = '
  • %s
  • '; } return $this->_href; } /** * 显示分页 */ function show($pHref=''){ $this->_default_href($pHref); if($this->mPageMax == 1) return ''; $tPage = array(); # 当前之前 $tMax = $this->mPageMax - $this->mPage > 5? 5: 10 - $this->mPageMax + $this->mPage; for ($i = 0; $i < $tMax; $i++) { if(($tNum = $this->mPage - $i) < 1) break; $tPage[] = $tNum; } $tPage && sort($tPage); # 当前之后 ($tMax = 10 - ($tCnt = count($tPage))) < 5 && $tMax = 5; ($tMax > ($this->mPageMax - $this->mPage)) && $tMax = $this->mPageMax - $this->mPage; for ($i = 0; $i < $tMax; $tPage[] = ++$i + $this->mPage); # 渲染分页 $tHtml = '
    '; } /** * 制造链接(逻辑写的不好,代码比较丑) * @param $p * @param $t * @return string */ function _make_href($p, $t){ if($p > $this->mPageMax) { $tReplace = array('%d'=>$this->mPage, '%s'=>$t); } elseif($this->mPage == $p){ $tReplace = array('">' => '" class="active">', '%d'=>$p, '%s'=>$t); } else { $tReplace = array('%d'=>$p, '%s'=>$t); } return strtr($this->_href, $tReplace); } }