php分页类
来源:互联网 发布:数据库设置主键的语句 编辑:程序博客网 时间:2024/06/07 23:34
class Paging
{
//记录的总条数
protected $total;
//每一页的条数
protected $pageSize;
//当前页
protected $page;
//页码的参数名字
protected $pageName;
//总页数
protected $pageCount;
//基本URL
protected $url;
/**
* [__construct description]
* @param [type] $total [总页数]
* @param integer $pageSize [每一页的条数]
* @param string $pageName [页码参数的名字]
*/
public function __construct($total, $pageSize=5, $pageName='page')
{
$this->total = $total;
$this->pageSize = $pageSize;
$this->pageCount = ceil($total / $pageSize);
$this->pageName = $pageName;
$this->url = $this->getUrl();
$this->page = $this->getPage();
}
public function headPage()
{
return $this->setUrl(1);
}
public function prevPage()
{
if ($this->page < 2) {
$page = 1;
} else {
$page = $this->page - 1;
}
return $this->setUrl($page);
}
public function nextPage()
{
if ($this->page < $this->pageCount) {
$page = $this->page + 1;
} else {
$page = $this->pageCount;
}
return $this->setUrl($page);
}
public function tailPage()
{
return $this->setUrl($this->pageCount);
}
public function givenPage($page)
{
if ($page < 1) {
$page = 1;
} else if ($page > $this->pageCount) {
$page = $this->pageCount;
}
return $this->setUrl($page);
}
public function listed()
{
return [
'head' => $this->headPage(),
'prev' => $this->prevPage(),
'next' => $this->nextPage(),
'tail' => $this->tailPage()
];
}
protected function getPage()
{
//我们约定,确定页码的参数默认是page
if (empty($_GET[$this->pageName])) {
return 1;
}
$page = (int)$_GET[$this->pageName];
if ($page < 1) {
$page = 1;
} else if ($page > $this->pageCount) {
$page = $this->pageCount;
}
return $page;
}
protected function getUrl()
{
//获取协议
$url = $_SERVER['REQUEST_SCHEME'] . '://';
//拼接主机
$url .= $_SERVER['HTTP_HOST'];
//拼接端口
$url .= ':' . $_SERVER['SERVER_PORT'];
//拼接URI
$requestUri = $_SERVER['REQUEST_URI'];
if (isset($_GET[$this->pageName])) {
$replaceStr = $this->pageName . '=' . $_GET[$this->pageName];
$replaceArr = [
$replaceStr.'&',
'&'.$replaceStr,
'?'.$replaceStr
];
$requestUri = str_replace($replaceArr, '', $requestUri);
}
return $url . $requestUri;
}
protected function setUrl($page)
{
if (strpos($this->url, '?')) {
return $this->url . '&' . $this->pageName . '=' . $page;
} else {
return $this->url . '?' . $this->pageName . '=' . $page;
}
}
{
//记录的总条数
protected $total;
//每一页的条数
protected $pageSize;
//当前页
protected $page;
//页码的参数名字
protected $pageName;
//总页数
protected $pageCount;
//基本URL
protected $url;
/**
* [__construct description]
* @param [type] $total [总页数]
* @param integer $pageSize [每一页的条数]
* @param string $pageName [页码参数的名字]
*/
public function __construct($total, $pageSize=5, $pageName='page')
{
$this->total = $total;
$this->pageSize = $pageSize;
$this->pageCount = ceil($total / $pageSize);
$this->pageName = $pageName;
$this->url = $this->getUrl();
$this->page = $this->getPage();
}
public function headPage()
{
return $this->setUrl(1);
}
public function prevPage()
{
if ($this->page < 2) {
$page = 1;
} else {
$page = $this->page - 1;
}
return $this->setUrl($page);
}
public function nextPage()
{
if ($this->page < $this->pageCount) {
$page = $this->page + 1;
} else {
$page = $this->pageCount;
}
return $this->setUrl($page);
}
public function tailPage()
{
return $this->setUrl($this->pageCount);
}
public function givenPage($page)
{
if ($page < 1) {
$page = 1;
} else if ($page > $this->pageCount) {
$page = $this->pageCount;
}
return $this->setUrl($page);
}
public function listed()
{
return [
'head' => $this->headPage(),
'prev' => $this->prevPage(),
'next' => $this->nextPage(),
'tail' => $this->tailPage()
];
}
protected function getPage()
{
//我们约定,确定页码的参数默认是page
if (empty($_GET[$this->pageName])) {
return 1;
}
$page = (int)$_GET[$this->pageName];
if ($page < 1) {
$page = 1;
} else if ($page > $this->pageCount) {
$page = $this->pageCount;
}
return $page;
}
protected function getUrl()
{
//获取协议
$url = $_SERVER['REQUEST_SCHEME'] . '://';
//拼接主机
$url .= $_SERVER['HTTP_HOST'];
//拼接端口
$url .= ':' . $_SERVER['SERVER_PORT'];
//拼接URI
$requestUri = $_SERVER['REQUEST_URI'];
if (isset($_GET[$this->pageName])) {
$replaceStr = $this->pageName . '=' . $_GET[$this->pageName];
$replaceArr = [
$replaceStr.'&',
'&'.$replaceStr,
'?'.$replaceStr
];
$requestUri = str_replace($replaceArr, '', $requestUri);
}
return $url . $requestUri;
}
protected function setUrl($page)
{
if (strpos($this->url, '?')) {
return $this->url . '&' . $this->pageName . '=' . $page;
} else {
return $this->url . '?' . $this->pageName . '=' . $page;
}
}
}
阅读全文
0 0
- 【PHP】php 分页类
- PHP分页代码 PHP分页类 PHP分页实例
- 【php基础】PHP分页类
- [PHP实例] PHP分页类
- PHP分页类
- php通用分页类
- 超强PHP分页类
- php分页类
- PHP多功能分页类
- php分页类使用方法
- pageft.php 分页类
- PHP 分页类
- php+oracle 分页类
- php分页类
- PHP分页类
- php分页类
- php分页类
- php分页类扩展
- 线性分类器
- LM method
- 国内android源码下载镜像及下载方法
- 六、自定义MVC
- 网易云课堂 互联网发展
- php分页类
- struct2 ajax
- Complex类的实现过程
- request.getContextPath()获取的是什么路径?
- Xpath总结
- 使用Sketch进行iOS开发
- CountDownLatch
- 微服务,微架构[十一]springboot模板页面thymeleaf
- java构造函数详解