使用方法
<?php
include 'page.class.php';
$object = new page(100,20);
$object->param=['title'=>'测试','name'=>'刘中胜'];
$data = $object -> setPage();
代码示例
<?php class page { //总数据条数 private $count; //每页显示条数默认20条 private $pageNum; //返回分页信息 private $config = []; //设置首页按钮名字 public $setHomeButtonName = '首页'; //设置上一页显示名称 public $setPreviousName = '上一页'; //设置下一页显示名称 public $setNextName = '下一页'; //设置尾页显示名称 public $setEndPageName = '尾页'; public $style = 1; public $param = []; public function __construct($count = 0, $pageNum = 10) { $this->count = $count; $this->pageNum = $pageNum; } public function setPage() { //获取当前分页数 $page = $this->getPage(); //显示多少条分页 $pageShowNum = 5; //获取总分页数 $totalPage = $this->getTotalPage(); //获取每页开始位置 $pageShowStart = $this->getPageStart($page, $pageShowNum); //检测分页显示数是否大于分页总数 if ($pageShowNum > $totalPage) { $pageShowNum = $totalPage; } //生成url $this->setHome($page); $this->setPrevious($page, $totalPage); $this->setNumPage($pageShowNum, $pageShowStart, $page); $this->setNext($page, $totalPage); $this->setEndPage($page, $totalPage); $this->config['total_page'] = $totalPage; $this->config['page_show_num'] = $pageShowNum; $this->config['page_show_start'] = $pageShowStart + 1; $this->config['page_show_end'] = $pageShowStart + $pageShowNum; $this->config['param'] = $this->param; return $this->config; } /** * 获取当前分页数 **/ private function getPage() { $page = @(int)$_GET['page']; if ($page == 0) { $page = 1; } return $page; } //获取总页数 private function getTotalPage() { return ceil($this->count / $this->pageNum); } //获取每页开始位置 private function getPageStart($page, $pageShowNum) { return (ceil($page / $pageShowNum) - 1) * $pageShowNum; } //生成首页按钮 private function setHome($page) { if ($this->style == 1 && $page <= 1) { $this->config['home'] = "<a href='javascript:;' class='home disable'>" . $this->setHomeButtonName . "</a>"; } else { $this->config['home'] = "<a href='?page=1" . $this->setParam() . "' class='home'>" . $this->setHomeButtonName . "</a>"; } return $this->config['home']; } //生成上一页按钮 private function setPrevious($page) { if ($this->style == 1 && $page <= 1) { $this->config['previous'] = "<a href='javascript:;' class='previous disable'>" . $this->setPreviousName . "</a>"; } else { $this->config['previous'] = "<a href='?page=" . ($page - 1) . $this->setParam() . "' class='previous'>" . $this->setPreviousName . "</a>"; } return $this->config['previous']; } //生成数字分页 private function setNumPage($pageShowNum, $pageShowStart, $page) { $url = ''; for ($i = 1; $i <= $pageShowNum; $i++) { $num = $pageShowStart + $i; if ($num == $page) { $url .= '<span style="color:red;">' . $num . '</span>'; } else { $url .= '<a href="?page=' . $num . $this->setParam() . '" class="num">' . $num . '</a>'; } } $this->config['num'] = $url; return $this->config['num']; } //生成下一页按钮 private function setNext($page, $totalPage) { if ($this->style == 1 && $totalPage <= $page) { $this->config['next'] = "<a href='javascript:;' class='next disable'>" . $this->setNextName . "</a>"; } else { $this->config['next'] = "<a href='?page=" . ($page + 1) . $this->setParam() . "' class='next'>" . $this->setNextName . "</a>"; } return $this->config['next']; } //生成结束按钮 private function setEndPage($page, $totalPage) { if ($this->style == 1 && $totalPage >= $page) { $this->config['end_page'] = "<a href='javascript:;' class='end_page disable'>" . $this->setEndPageName . "</a>"; } else { $this->config['end_page'] = "<a href='?page=" . $totalPage . $this->setParam() . "' class='end_page'>" . $this->setEndPageName . "</a>"; } return $this->config['end_page']; } //设置参数 private function setParam() { $paramData = $this->param; if (!empty($paramData)) { $param = ''; foreach ($paramData as $key => $value) { $param .= '&'.$key.'='.urlencode($value); } } else { $param = ''; } return $param; } }