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;
}

thinkphp5 原生sql带分页方法的更多相关文章

  1. JPA或Hibernate中使用原生SQL实现分页查询、排序

    发生背景:前端展示的数据需要来自A表和D表拼接,A表和D表根据A表的主键进行关联,D表的非主键字段关联C表的主键,根据条件筛选出符合的数据,并且根据A表的主键关联B表的主键(多主键)的条件,过滤A表中 ...

  2. SQL Server游标 C# DataTable.Select() 筛选数据 什么是SQL游标? SQL Server数据类型转换方法 LinQ是什么? SQL Server 分页方法汇总

    SQL Server游标   转载自:http://www.cnblogs.com/knowledgesea/p/3699851.html. 什么是游标 结果集,结果集就是select查询之后返回的所 ...

  3. thinkPHP框架中执行原生SQL语句的方法

    这篇文章主要介绍了thinkPHP框架中执行原生SQL语句的方法,结合实例形式分析了thinkPHP中执行原生SQL语句的相关操作技巧,并简单分析了query与execute方法的使用区别,需要的朋友 ...

  4. SQL SERVER 分页方法

    最近项目中需要在SQL SERVER中进行分页,需要编写分页查询语句.之前也写过一些关于分页查询的语句,但是性能不敢恭维.于是在业务时间,在微软社区Bing了一篇老外写的关于SQL SERVER分页的 ...

  5. SQL server 分页方法小结

    这里面介绍一下常用的分页方法: 1.使用top来分页 select top @pageSize * from table where id not in (select top @pageSize*( ...

  6. SQL Server 分页方法汇总

    PageSize = 30 PageNumber = 201 方法一:(最常用的分页代码, top / not in) UserId UserId from UserInfo order by Use ...

  7. SQL 相关分页方法

    [1] SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER OFFGO ALTER PROCEDURE [dbo].[procCom_Get_Pagination]( @ ...

  8. thinkphp 原生sql使用分页类

    public function index(){ import("@.ORG.Page"); //导入分页类 $Dao = M(); $count = count($Dao-> ...

  9. Oracle、SQL Server、MySQL分页方法

    测试用例:查询TEST_TABLE表中TEST_COLUMN列的第10-20条数据 1,Oracle分页方法 SELECT A.* FROM ( SELECT ROWNUM ROWNO, B.* FR ...

随机推荐

  1. lsnrctl start 报错

    lsnrctl start报错: TNS-12541:TNS:no listener TNS-12560:TNS:protocol adapter error TNS-00511:No listene ...

  2. Linux shell是什么

    shell概念: shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用shell启动,挂起,停止甚至编写一些程序. shell还是一个功能强 ...

  3. c++性能测试工具:google benchmark进阶(一)

    这是c++性能测试工具教程的第四篇文章,从本篇开始我将逐步介绍一些性能测试的高级技巧. 前三篇教程可以看这里: c++性能测试工具:google benchmark入门(一) c++性能测试工具:go ...

  4. Spring Boot 2.x基础教程:如何扩展XML格式的请求和响应

    在之前的所有Spring Boot教程中,我们都只提到和用到了针对HTML和JSON格式的请求与响应处理.那么对于XML格式的请求要如何快速的在Controller中包装成对象,以及如何以XML的格式 ...

  5. 在vue项目中使用echarts

    1.安装echarts依赖npm install echarts --save 2.在要使用的页面引入import echarts from 'echarts'v5之后使用 import * echa ...

  6. javascript之一切都是对象

    在学习的过程中,我们常常能听到这样一句话:一切皆是对象.那么这句话该如何理解呢?首先,我们要明确对象的概念.要明白除了基本数据类型都是对象. typeof操作符是大家经常使用的,我们常用它来检测给定变 ...

  7. .Net Core 3.1简单搭建微服务

    学如逆水行舟,不进则退!最近发现微服务真的是大势所趋,停留在公司所用框架里已经严重满足不了未来的项目需要了,所以抽空了解了一下微服务,并进行了代码落地. 虽然项目简单,但过程中确实也学到了不少东西. ...

  8. json串向后台传递数值自动四舍五入的问题

    业务需求:传递前台输入的数据,数量要求是小数点(多条数据) 后台服务是使用asp.net写的. 问题:反序列化JSON时总是自动四舍五入. 原因:JSON反序列化的时候数据类型是以第一条数据的类型为准 ...

  9. 解决pip安装时出现报错TypeError unsupported operand type(s) for -= 'Retry' and 'int'

    1.参考 https://stackoverflow.com/questions/42610545/typeerror-unsupported-operand-types-for-retry-and- ...

  10. Arduino IDE 2.0 beta安装

    1.在官网(Software | Arduino)下载安装包,此次提供操作系统有:Windows.Linux和macOC系统 2.点击安装包进行安装 3.点击我同意 4.点击下一步 5.选择安装路径( ...