分页类Page.php
<?php
class Page
{
private $curPage;//当前页
private $totalNum;
private $requestUrl;
private $params;//分页时带上查询参数
private $totalPage;
private $queryString;//查询条件字符串
public function __construct($curPage = 1, $totalNum = 0, $pageSize = 50, $requestUrl = '', $params = [])
{
$this->curPage = $curPage;
$this->requestUrl = $requestUrl;
$this->params = $params;
$this->totalNum = $totalNum;
$this->totalPage = ceil($totalNum / $pageSize);
$this->queryString = http_build_query($params);
}
public function showPage()
{
$html = "<div class='pagination'>";
$html .= $this->showTotalNum();
$html .= $this->prePage();
$html .= $this->pageList();
$html .= $this->nextPage();
$html .= "</div>";
return $html;
}
//显示总条数
private function showTotalNum()
{
$html = "<li class='disabled'><span>总条数 {$this->totalNum}</span></li>";
return $html;
}
//上一页
private function prePage()
{
if ($this->curPage == 1) {
$html = "<li class='disabled'><span>«</span></li>";
} else {
$prePage = $this->curPage - 1;
$html = "<li><a href=\"{$this->requestUrl}?page=$prePage&$this->queryString\">«</a></li>";
}
return $html;
}
//页码列表
private function pageList()
{
$html = '';
if ($this->curPage < 7) {
if ($this->totalPage > 10) {
$lastPage1 = $this->totalPage;
$lastPage2 = $this->totalPage - 1;
for ($i = 1; $i <= 8; $i++) {
if ($this->curPage == $i) {
$html .= "<li class='active'><span>$i</span></li>";
} else {
$html .= "<li><a href=\"{$this->requestUrl}?page=$i&$this->queryString\">$i</a></li>";
}
}
$html .= "<li class='disabled'><span>...</span></li>";
$html .= "<li><a href=\"{$this->requestUrl}?page=$lastPage1&$this->queryString\">$lastPage2</a></li>";
$html .= "<li><a href=\"{$this->requestUrl}?page=$lastPage2&$this->queryString\">$lastPage1</a></li>";
} else {
for ($i = 1; $i <= $this->totalPage; $i++) {
if ($this->curPage == $i) {
$html .= "<li class='active'><span>$i</span></li>";
} else {
$html .= "<li><a href=\"{$this->requestUrl}?page=$i&$this->queryString\">$i</a></li>";
}
}
}
} else if ($this->curPage >= 7) {
$html .= "<li><a href=\"{$this->requestUrl}?page=1&$this->queryString\">1</a></li>";
$html .= "<li><a href=\"{$this->requestUrl}?page=2&$this->queryString\">2</a></li>";
$html .= "<li class='disabled'><span>...</span></li>";
$curPage = $this->curPage;
$totalPage = $this->totalPage;
if ($totalPage >= 13 && ($totalPage - $curPage) > 5) {
for ($i = $curPage - 3; $i <= $curPage + 3; $i++) {
if ($i == $curPage) {
$html .= "<li class='active'><span>$i</span></li>";
} else {
$html .= "<li><a href=\"{$this->requestUrl}?page=$i&$this->queryString\">$i</a></li>";
}
}
$lastPage1 = $totalPage;
$lastPage2 = $totalPage - 1;
$html .= "<li class='disabled'><span>...</span></li>";
$html .= "<li><a href=\"{$this->requestUrl}?page=$lastPage1&$this->queryString\">$lastPage2</a></li>";
$html .= "<li><a href=\"{$this->requestUrl}?page=$lastPage2&$this->queryString\">$lastPage1</a></li>";
} else {
for ($i = $curPage - 3; $i <= $this->totalPage; $i++) {
if ($i == $curPage) {
$html .= "<li class='active'><span>$i</span></li>";
} else {
$html .= "<li><a href=\"{$this->requestUrl}?page=$i&$this->queryString\">$i</a></li>";
}
}
}
}
return $html;
}
//下一页
private function nextPage()
{
$html = '';
if ($this->totalPage != 1) {
$nextPage = $this->curPage + 1;
$html = "<li><a href=\"{$this->requestUrl}?page=$nextPage&$this->queryString\">»</a></li>";
}
return $html;
}
}
控制器中调用
//引入文件
require_once(CMF_ROOT.'simplewind/extend/paginate/Page.php');
//总数据条数
$totalNum = 1000;
//不带参数的请求地址
$requestUrl = '/Admin/pdf_word_user/index.html';
$page = new \Page($pgnum,$totalNum,$pgsize,$requestUrl,['a'=>123,'b'=>456]);
$pageHtml = $page->showPage();
$this->assign("pageHtml", $pageHtml);
return $this->fetch();
视图层程现样式