Skip to content

Instantly share code, notes, and snippets.

@ss22219
Last active December 24, 2015 22:29
Show Gist options
  • Save ss22219/6873495 to your computer and use it in GitHub Desktop.
Save ss22219/6873495 to your computer and use it in GitHub Desktop.
<?php
/**
* author: gool
**/
//分页信息类,移植自C#
class BasePage {
///当前页号
private $pageIndex = 1;
///每页显示数量
private $pageSize = 1;
///显示页标数
private $pageLabelCount = 4;
///总数量
private $totalCount = 1;
///当前页列表
private $list;
/**
* 取得内容列表
* @return type
*/
public function getList() {
return $this->list;
}
public function setList($list) {
$this->list = $list;
}
/**
* 取得当前页号
* @return type
*/
public function getPageIndex() {
return $this->pageIndex;
}
public function setPageIndex($pageIndex) {
if ($pageIndex >= 1) {
$this->pageIndex = $pageIndex;
}
}
/**
* 取得每页显示数量
* @return type
*/
public function getPageSize() {
return $this->pageSize;
}
public function setPageSize($pageSize) {
if ($pageSize >= 1) {
$this->pageSize = $pageSize;
}
}
/**
* 取得显示页表数量
* @param type $pageSize
*/
public function getPageLabelCount() {
return $this->pageLabelCount;
}
public function setPageLabelCount($pageLabelCount) {
if ($pageLabelCount >= 1) {
$this->pageLabelCount = $pageLabelCount;
}
}
/**
* 取得内容总数量
* @param type $pageSize
*/
public function getTotalCount() {
return $this->totalCount;
}
public function setTotalCount($totalCount) {
if ($totalCount > 0) {
$this->totalCount = $totalCount;
}
}
/**
* 取得最大页数
* @param type $pageSize
*/
public function getTotalPage() {
return ceil($this->getTotalCount() / (double) $this->getPageSize()) <= 0 ? 1 : ceil($this->getTotalCount() / $this->getPageSize());
}
/**
* 取得页标列表
* @param type $pageSize
*/
public function getPageLabels() {
$this->setPageIndex($this->getPageIndex() > $this->getTotalPage() ? $this->getTotalPage() : $this->getPageIndex());
if ($this->getPageLabelCount() == 1) {
return array($this->getPageIndex());
}
$list = array();
$startPage = 0;
$endPage = 0;
if ($this->getPageLabelCount() == 2) {
$startPage = $this->getPageIndex() == $this->getTotalPage() ? $this->getPageIndex() - 1 : $this->getPageIndex();
$startPage = $startPage < 1 ? 1 : $startPage;
$endPage = $startPage + 1;
$endPage = $endPage > $this->getTotalPage() ? $this->getTotalPage() : $endPage;
} else if ($this->getPageLabelCount() % 2 == 0) {
$labelPage = floor(($this->getPageIndex() - 2) / ($this->getPageLabelCount() - 2)) + 1;
$startPage = ($labelPage - 1) * ($this->getPageLabelCount() - 2) + 1;
$startPage = $startPage < 1 ? 1 : $startPage;
$startPage = $startPage > $this->getTotalPage() ? $this->getTotalPage() : $startPage;
$endPage = $startPage + $this->getPageLabelCount() - 1;
$endPage = $endPage > $this->getTotalPage() ? $this->getTotalPage() : $endPage;
$endPage = $endPage < 1 ? 1 : $endPage;
} else {
$startPage = $this->getPageIndex() - (($this->getPageLabelCount() - 1) / 2);
$startPage = $startPage < 1 ? 1 : $startPage;
$startPage = $startPage > $this->getTotalPage() ? $this->getTotalPage() : $startPage;
$endPage = $startPage + $this->getPageLabelCount() - 1;
$endPage = $endPage > $this->getTotalPage() ? $this->getTotalPage() : $endPage;
$endPage = $endPage < 1 ? 1 : $endPage;
}
for ($i = $startPage; $i <= $endPage; $i++) {
$list[] = $i;
}
return $list;
}
/**
* 生成一个分页信息类
* @param array $list 当前页内容列表
* @param int $pageIndex 当前页数
* @param int $pageSize 每页显示数量
* @param int $totalCount 总数量
* @param int $labelCount 显示页表数
*/
public function __construct($list, $pageIndex, $pageSize, $totalCount, $labelCount) {
$this->setList($list);
$this->setPageSize($pageSize);
$this->setPageIndex($pageIndex);
$this->setPageLabelCount($labelCount);
$this->setTotalCount($totalCount);
}
}
//Php分页类
class PhpPager extends BasePage
{
public $template,$currentTemplate,$showHome,$homeName = '&lt;&lt;',$showEnd,$endName = '...';
public function __construct($pageSize, $totalCount,$template = '',$currentTemplate = '',$prefix = 'page', $labelCount = 10){
$this->template = $template;
$this->currentTemplate = $currentTemplate;
$pageIndex = empty($_GET[$prefix]) ? 1 : intval($_GET[$prefix]);
$pageIndex = $pageIndex < 0 ? 1 :$pageIndex;
parent::__construct(NULL, $pageIndex, $pageSize, $totalCount, $labelCount);
}
public function generate(){
$str = '';
$lables = $this->getPageLabels();
if($this->showHome && $this->getPageIndex() > 0){
$home = str_replace('{page}',1,$this->template);
$home = str_replace('{name}',$this->homeName,$home);
$str .= $home;
}
foreach ($lables as $label)
{
if($label == $this->getPageIndex()){
$str.= str_replace(array('{page}','{name}'),$label,$this->currentTemplate);
}else{
$str.= str_replace(array('{page}','{name}'),$label,$this->template);
}
}
if($this->showEnd && $this->getPageIndex() < $this->getTotalPage()){
$end = str_replace('{page}',$this->getTotalPage(),$this->template);
$end = str_replace('{name}',$this->endName,$end);
$str .= $end;
}
return $str;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment