1 - 下载 Swoole Loader 请下载 WINNT 系统 PHP-7.4.33 版本 非线程安全 的 swoole_loader 扩展,点击直达下载页面 2 - 安装 Swoole Loader 将刚才下载的 swoole_loader 扩展文件( swoole_loader.dll )上传到当前 PHP 的扩展安装目录中: ext......
ThinkPHP5 自定义分页模板
用ThinkPHP5实现百度分页功能(案例提供了首页和末页功能,实际开发中建议去掉。因为末页功能,页数越往后, mysql过滤的条数变少,执行时间就越来越长。大多数情况下,我们关心前几页数据,很少关心后面的数据。)
工具/原料
- 开发环境 PHP5.6 MySQL5.7
方法/步骤
ThinkPHP默认的分页模板
自定义分页模板
实现代码
?php
namespace tool;
use think\Paginator;
class TpPageHelper extends Paginator
{
//TODO : 中间按钮个数
protected $bnumber = 10;
//TODO : 首页
protected function getfirstButton($str = '')
{
if ($this->currentPage() = 1) {
return $this->getDisabledTextWrapper($str);
}
$url = $this->url(1);
return $this->getPageLinkWrapper($url, $str);
}
//TODO : 上一页
protected function getPreviousButton($text = "«")
{
if ($this->currentPage() = 1) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url(
$this->currentPage() - 1
);
return $this->getPageLinkWrapper($url, $text);
}
//TODO : 页码
protected function getLinks()
{
if ($this->total > $this->listRows) {
if ($this->lastPage $this->bnumber) {
return $this->getUrlLinks($this->getUrlRange(1, $this->lastPage));
} else {
$min = 1;
if ($this->currentPage > $this->bnumber / 2) $min = $this->currentPage - floor($this->bnumber / 2);
if ($this->lastPage - $this->currentPage $this->bnumber / 2) $min = $this->lastPage - $this->bnumber 1;
return $this->getUrlLinks($this->getUrlRange($min, $min $this->bnumber - 1));
}
}
}
//TODO : 下一页
protected function getNextButton($text = '»')
{
if (!$this->hasMore) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url($this->currentPage() 1);
return $this->getPageLinkWrapper($url, $text);
}
//TODO : 末页
protected function getlastButton($text = '')
{
if (!$this->hasMore) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url($this->lastPage());
return $this->getPageLinkWrapper($url, $text);
}
//TODO : 渲染页
public function render()
{
//数据是否足够分页
if ($this->hasPages()) {
return sprintf(
'ul class="btn-item fr">%s %s %s %s %s/ul>',
$this->getfirstButton('首页'),
$this->getPreviousButton('上一页'),
$this->getLinks(),
$this->getNextButton('下一页'),
$this->getlastButton('末页')
);
}
}
//TODO : 生成禁用按钮
protected function getDisabledTextWrapper($text)
{
return 'li class="disabled">span>' . $text . '/span>/li>';
}
//TODO : 生成普通按钮
protected function getPageLinkWrapper($url, $page)
{
if ($page == $this->currentPage()) {
return $this->getActivePageWrapper($page);
}
return $this->getAvailablePageWrapper($url, $page);
}
//TODO : 生成当前页按钮
protected function getActivePageWrapper($text)
{
return 'li class="active">span>' . $text . '/span>/li>';
}
//TODO : 可点击按钮
protected function getAvailablePageWrapper($url, $page)
{
return 'li>a href="' . htmlentities($url) . '">' . $page . '/a>/li>';
}
//TODO : 批量生成页码按钮
protected function getUrlLinks(array $urls)
{
$html = '';
foreach ($urls as $page => $url) {
$html .= $this->getPageLinkWrapper($url, $page);
}
return $html;
}
}
4.1 将 TpPageHelper.php 放在extend\tool下
4.2 修改默认模板
4.3 项目链接:http://pan.baidu.com/s/1miIIcmG密码:wwnl
以上方法由办公区教程网编辑摘抄自百度经验可供大家参考!
标签: PHP
相关文章