1、先在頂部引入use think\paginator\driver\Page;
2、使用下例程式碼
$pageNumber = input('page')? input('page'):'0';//客戶端傳過來的分頁
if($pageNumber > 0){
$pageNumber_one = $pageNumber-1;
}else{
$pageNumber_one = 0;
}
$limit = 20;//每頁顯示條數;
$offset= $pageNumber_one*$limit;//查詢偏移值//查詢數
$list = Db::query("SELECT * FROM `表名` LIMIT $offset,$limit ");
$count_data = Db::query('SELECT count(*) FROM `表名` ');
$count = $count_data['0']['count(*)'];//組合分頁資料格式
$this->assign('count',$count);
//組合分頁資料格式
$pagernator = Page::make($list,$limit,$pageNumber,$count,false,['path'=>Page::getCurrentPath(),'query'=>request()->param()]);
$this->assign('datalist',$list);
$this->assign('page',$pagernator->render());
3、think\paginator\driver\Page.php的程式碼如下
<?php namespace think\paginator\driver; use think\Paginator; class Page extends Paginator
{
//首頁
protected function home($text = "首頁") {
if ($this->currentPage() <= 1) {
return $this->getDisabledTextWrapper($text);
} $url = $this->url(1); return $this->getPageLinkWrapper($url, $text);
} /**
* 上一頁按鈕
* @param string $text
* @return string
*/
protected function getPreviousButton($text = "上一頁")
{ if ($this->currentPage() <= 1) {
return $this->getDisabledTextWrapper($text);
} $url = $this->url($this->currentPage() - 1); return $this->getPageLinkWrapper($url, $text); }
/**
* 下一頁按鈕
* @param string $text
* @return string
*/
protected function getNextButton($text = '下一頁')
{
if (!$this->hasMore) {
return $this->getDisabledTextWrapper($text);
} $url = $this->url($this->currentPage() + 1); return $this->getPageLinkWrapper($url, $text);
}
//尾頁
protected function last($text = '尾頁') {
if (!$this->hasMore) {
return $this->getDisabledTextWrapper($text);
} $url = $this->url($this->lastPage); return $this->getPageLinkWrapper($url, $text);
}
/**
* 頁碼按鈕
* @return string
*/
protected function getLinks()
{
if ($this->simple)
return ''; $block = [
'first' => null,
'slider' => null,
'last' => null
]; $side = 3;
$window = $side * 2; if ($this->lastPage < $window + 6) {
$block['first'] = $this->getUrlRange(1, $this->lastPage);
} elseif ($this->currentPage <= $window) {
$block['first'] = $this->getUrlRange(1, $window + 2);
$block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
} elseif ($this->currentPage > ($this->lastPage - $window)) {
$block['first'] = $this->getUrlRange(1, 2);
$block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);
} else {
$block['first'] = $this->getUrlRange(1, 2);
$block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
$block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
} $html = ''; if (is_array($block['first'])) {
$html .= $this->getUrlLinks($block['first']);
} if (is_array($block['slider'])) {
$html .= $this->getDots();
$html .= $this->getUrlLinks($block['slider']);
} if (is_array($block['last'])) {
$html .= $this->getDots();
$html .= $this->getUrlLinks($block['last']);
} return $html;
} /**
* 渲染分頁html
* @return mixed
*/
public function render()
{
if ($this->lastPage > 1) {
if ($this->hasPages()) {
if ($this->simple) {
return sprintf(
'<div class="pagination">%s %s %s %s</div>',
$this->home(),
$this->getPreviousButton(),
$this->getNextButton(),
$this->last()
);
} else {
return sprintf(
'<div class="pagination">%s %s %s %s %s</div>',
$this->home(),
$this->getPreviousButton(),
$this->getLinks(),
$this->getNextButton(),
$this->last()
);
}
}
}
} /**
* 生成一個可點選的按鈕
*
* @param string $url
* @param int $page
* @return string
*/
protected function getAvailablePageWrapper($url, $page)
{
return '<a href="' . htmlentities($url) . '" title="第'. $page .'頁" >' . $page . '</a>';
} /**
* 生成一個禁用的按鈕
*
* @param string $text
* @return string
*/
protected function getDisabledTextWrapper($text)
{
return '<span>' . $text . '</span>';
} /**
* 生成一個啟用的按鈕
*
* @param string $text
* @return string
*/
protected function getActivePageWrapper($text)
{
return '<a href="" class="cur">' . $text . '</a>';
} /**
* 生成省略號按鈕
*
* @return string
*/
protected function getDots()
{
//return $this->getDisabledTextWrapper('...');
} /**
* 批量生成頁碼按鈕.
*
* @param array $urls
* @return string
*/
protected function getUrlLinks(array $urls)
{
$html = '';
$i = 1;
foreach ($urls as $page => $url) {
if($this->currentPage() == 0){
if($i == 1) {
$html .= $this->getActivePageWrapper(1);
}else{
$html .= $this->getPageLinkWrapper($url, $page);
}
}else{
$html .= $this->getPageLinkWrapper($url, $page);
}
$i++;
}
return $html;
} /**
* 生成普通頁碼按鈕
*
* @param string $url
* @param int $page
* @return string
*/
protected function getPageLinkWrapper($url, $page)
{
if ($page == $this->currentPage()) {
return $this->getActivePageWrapper($page);
} return $this->getAvailablePageWrapper($url, $page);
}
}
4、分頁按鈕樣式
.pagination span{
margin:0;
cursor:pointer
}
.pagination{
padding:0;
margin:20px 0;text-align: center;
}
.pagination a{
margin-right:10px;
padding:6px 12px;
border:1px #cccccc solid;
background:#fff;
text-decoration:none;
color:#808080;
font-size:12px;
line-height:24px;
}
.pagination a:hover{
color:#fc6d41;
background: white;
border:1px #fc6d41 solid;
}
.pagination a.cur{
border:1px #fc6d41 solid;
background:#fc6d41;
color:#fff;padding:6px 12px;
}
.pagination span{
padding:6px 12px;
font-size:12px;
line-height:24px;
color:#bbb;
border:1px #ccc solid;
background:#fcfcfc;
margin-right:8px; }
.pagination span.pageRemark{
border-style:none;
background:none;
margin-right:0px;
padding:4px 0px;
color:#666;
}
.pagination span.pageRemark b{
color:red;
}
.pagination span.pageEllipsis{
border-style:none;
background:none;
padding:4px 0px;
color:#808080;
}