<?php
/**
* 简单的分页类
* author 马鑫亮
*/
class Pagination
{
/**
* 当前页
*/
private $page = 0;
/**
* 每页显示条数
*/
private $page_size = 0;
/**
* 总条数
*/
private $total = 0;
public function __construct($total, $page, $page_size)
{
$this->total = $total;
$this->page = $page;
$this->page_size = $page_size;
}
/**
* 取得总页数
* @return void
*/
protected function getPages()
{
return ceil($this->total / $this->page_size);
}
/**
* 取得下一页
* @return void
*/
protected function getNextPage()
{
return ($this->page >= $this->getPages()) ? $this->getPages() : ($this->page + 1);
}
/**
* 取得上一页
* @return void
*/
protected function getPrevPage()
{
return ($this->page <= 1) ? 1 : ($this->page - 1);
}
/**
* 取得序列的分页信息 如:上一页 1 2 3 下一页
* @step init 每次显示的页数 默认为:3
* @return array
*/
public function getPageRange($step = 3)
{
$range = array();
$pages = $this->getPages();
$this->page = ($this->page >= $pages) ? $pages : $this->page;
$step = min($pages, $step);
$low = (($this->page - 1) % $step) ? ($this->page - (($this->page - 1) % $step)) : $this->page;
$high = ($this->page >= $pages) ? $pages : ($low + $step - 1);
$range = range($low, $high);
return array(
"page" => $this->page,
"prev_page" => $this->getPrevPage(),
"next_page" => $this->getNextPage(),
"total" => $this->total,
"pages" => $pages,
"range" => $range
);
}
/**
* 取得传统的分页信息 如:首页 上一页 下一页 末页
* @return array
*/
public function getPageInfo()
{
return array(
"page" => ($this->page >= $pages) ? $pages : $this->page,
"prev_page" => $this->getPrevPage(),
"next_page" => $this->getNextPage(),
"total" => $this->total,
"pages" => $this->getPages()
);
}
}
$pagination = new Pagination(200, 20, 10);
print_r($pagination->getPageRange(4));
?>
简单php分页类
最新推荐文章于 2025-02-13 09:09:02 发布