thinkphp5 原生sql带分页方法
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带分页方法的更多相关文章
- JPA或Hibernate中使用原生SQL实现分页查询、排序
发生背景:前端展示的数据需要来自A表和D表拼接,A表和D表根据A表的主键进行关联,D表的非主键字段关联C表的主键,根据条件筛选出符合的数据,并且根据A表的主键关联B表的主键(多主键)的条件,过滤A表中 ...
- SQL Server游标 C# DataTable.Select() 筛选数据 什么是SQL游标? SQL Server数据类型转换方法 LinQ是什么? SQL Server 分页方法汇总
SQL Server游标 转载自:http://www.cnblogs.com/knowledgesea/p/3699851.html. 什么是游标 结果集,结果集就是select查询之后返回的所 ...
- thinkPHP框架中执行原生SQL语句的方法
这篇文章主要介绍了thinkPHP框架中执行原生SQL语句的方法,结合实例形式分析了thinkPHP中执行原生SQL语句的相关操作技巧,并简单分析了query与execute方法的使用区别,需要的朋友 ...
- SQL SERVER 分页方法
最近项目中需要在SQL SERVER中进行分页,需要编写分页查询语句.之前也写过一些关于分页查询的语句,但是性能不敢恭维.于是在业务时间,在微软社区Bing了一篇老外写的关于SQL SERVER分页的 ...
- SQL server 分页方法小结
这里面介绍一下常用的分页方法: 1.使用top来分页 select top @pageSize * from table where id not in (select top @pageSize*( ...
- SQL Server 分页方法汇总
PageSize = 30 PageNumber = 201 方法一:(最常用的分页代码, top / not in) UserId UserId from UserInfo order by Use ...
- SQL 相关分页方法
[1] SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER OFFGO ALTER PROCEDURE [dbo].[procCom_Get_Pagination]( @ ...
- thinkphp 原生sql使用分页类
public function index(){ import("@.ORG.Page"); //导入分页类 $Dao = M(); $count = count($Dao-> ...
- Oracle、SQL Server、MySQL分页方法
测试用例:查询TEST_TABLE表中TEST_COLUMN列的第10-20条数据 1,Oracle分页方法 SELECT A.* FROM ( SELECT ROWNUM ROWNO, B.* FR ...
随机推荐
- DOS命令行(9)——wmic-系统管理命令行工具
wmic 介绍与语法 WMI(Windows Management Instrumentation,Windows 管理规范)是一项核心的 Windows 管理技术:用户可以使用 WMI 管理本地和远 ...
- 本地SQL Server怎么连接服务器上的数据库
在阿里云的安全组规则中的入方向加上端口1433就好了,首先得要有这个,其他的都是后话
- Windows平台安装RabbitMQ(亲测)
一.下载安装包 https://www.rabbitmq.com/download.html 选择Windows下载 3.下载RabbitMQ安装包和运行环境Erlang安装包 (1)比对下载对应的版 ...
- Netty 框架学习 —— 预置的 ChannelHandler 和编解码器
Netty 为许多提供了许多预置的编解码器和处理器,几乎可以开箱即用,减少了在烦琐事务上话费的时间和精力 空闲的连接和超时 检测空闲连接以及超时对于释放资源来说至关重要,Netty 特地为它提供了几个 ...
- excel函数提取身份证出生日期,分离日期时间的日期和时间
1.提取身份证出生日期 =1*TEXT(MID(H13,7,8),"0-00-00")用MID函数提取表示日期的位数,再用text函数转换为格式1998-6-21格式的文本,再通过 ...
- 怎样用好PS中的钢笔工具(附练习钢笔工具网站)
想要在PS中得心应手的的描绘出自己想要的线条(也就是路径),就需要对[钢笔工具]有一个充分的理解. [钢笔工具]绘出来的线条全部都是贝赛尔曲线,所以你在学习[钢笔工具]之前,要补习一下贝赛尔曲线的常识 ...
- 2.14、制作Centos模板及优化操作
1.安装系统: (1)规范网卡为eth0的模式: (2)选择时区: (3)选择支持的语言包含中文: (4)选择软件包: 补充:桌面版包选择: (5)关闭down机内存日志: (6)分区: 1)分区说明 ...
- 14、web服务器介绍
14.1.用户访问网站流程: 1. dns解析原理: 客户端到dns服务器之间的查询为递归查询: dns服务器到根域名服务器的查询是迭代查询: [lc@m01 ~]$ dig www.baidu.co ...
- UVA 11475 Extend to Palindrome hash
题意: 给出一个字符串,让你往后添加最少的字符,使其成为回文串. 分析: 题目就相当于求后缀字符串为回文串的最长长度,判断回文串要O(n)时间,直接判断肯定不行.我们从后往前枚举,每次字符串与上一个字 ...
- centos 关闭SELINUX并重启系统
关闭SELINUX [root@bogon ~]# vim /etc/sysconfig/selinux ... SELINUX=disabled ... 执行过程: 重启系统 [root@bogo ...