TP5分页类

<?php
class Page {
public $page; //当前页
public $total; //总记录数
public $listRows; //每页显示记录数
private $uri;//动态url
public $pageNum; //总页数
private $listNum=6;//显示页码按钮数量
public $render;//分页后的html模板
public $data;//分页后渲染到模板的数据
/*
* 初始化分页数据
*$sdata 待分页的数据
* $listRows 每页记录数
*/
public function __construct($sdata, $listRows=15){
$this->total=count($sdata);
$this->listRows=$listRows;
$this->uri=$this->getUri();
$this->page=!empty($_GET["page"]) ? $_GET["page"] : 1;
$this->pageNum=ceil($this->total/$this->listRows);
$this->render=$this->pageHtml();
$this->data=array_slice($sdata,($this->page-1)*$this->listRows,$listRows);
return $this->data;
}
//动态获取url
private function getUri(){
$url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?");
$parse=parse_url($url);
if(isset($parse["query"])){
parse_str($parse['query'],$params);
unset($params["page"]);
$url=$parse['path'].'?'.http_build_query($params);
}
return $url;
}
//首页
private function first(){
$html = "";
if($this->page==1)
$html.=" <a style='magin=10px;' class='current btn disabled'>首 页</a>";
else
$html.=" <a class='btn btn-primary-outline' href='{$this->uri}&page=1'>首 页</a>";
return $html;
}
//上一页
private function prev(){
$html = "";
if($this->page==1)
$html.=" <a class='current btn disabled'>上一页</a>";
else
$html.=" <a class='btn btn-primary-outline' href='{$this->uri}&page=".($this->page-1)."'>上一页</a>";
return $html;
}
//页码按钮
private function pageList(){
$linkPage="";
$inum=floor($this->listNum/2);
for($i=$this->page-$inum;$i<=$this->page+$inum;$i++){
if($i<=0){
continue;
}
if($i>$this->pageNum){
continue;
}
if($i == $this->page){
$linkPage.=" <a class='current btn btn-secondary'>{$i}</a>";
}else{
$linkPage.=" <a class='btn btn-primary-outline' href='{$this->uri}&page={$i}'>{$i}</a>";
}
}
return $linkPage;
}
//下一页
private function next(){
$html = "";
if($this->page==$this->pageNum)
$html.=" <a class='current btn disabled'>下一页</a>";
else
$html.=" <a class='btn btn-primary-outline' href='{$this->uri}&page=".($this->page+1)."'>下一页</a>";
return $html;
}
//尾页
private function last(){
$html = "";
if($this->page==$this->pageNum)
$html.=" <a class='current btn disabled'>尾 页</a>";
else
$html.=" <a class='btn btn-primary-outline' href='{$this->uri}&page=".($this->pageNum)."'>尾 页</a>";
return $html;
}
//输入指定页码
private function goPage(){
return ' <input class="input-text" type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=\''.$this->uri.'&page=\'+page+\'\'}" value="'.$this->page.'" style="width:52px"><input class="btn btn-secondary" type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'&page=\'+page+\'\'"> ';
}
//选择指定页码
function selectPage(){
$inum=10;
$location = $this->uri.'&page=';
$selectPage ="<span class='va-m'>到第 </span> <span class='select-box' style='width:initial'><select class='select' name='topage' size='1' onchange='window.location=\"$location\"+this.value'>";
for($i=$this->page-$inum;$i<=$this->page+$inum;$i++){
if($i<=0){
continue;
}
if($i>$this->pageNum){
continue;
}
if($i == $this->page){
$selectPage .="<option value='$i' selected>$i</option>";
}else{
$selectPage .="<option value='$i'>$i</option>";
}
}
$selectPage .="</select></span> <span class='va-m'>页</span>";
return $selectPage;
}
//组装分页的html模板
function pageHtml(){
$html = "<div class='cl mt-20 text-c'>";
// $html .= "<span class='pr-20 va-m'>共有<b>{$this->total}</b>条记录</span>";
// $html .= "<span class='pr-20 va-m'>每页显示<b>{$this->listRows}</b>条</span>";
// $html .= "<span class='pr-20 va-m'><b>当前{$this->page}/{$this->pageNum}</b>页</span>";
$html .= $this->first();
$html .= $this->prev();
$html .= $this->pageList();
$html .= $this->next();
$html .= $this->last();
$html .= $this->goPage();
$html .= $this->selectPage();
$html .= '</div>';
return $html;
}
}
使用方法:在控制器中调用这个扩展类,new一个分页对象$p,并渲染到模板
//$data:array,通过select()查询未分页的数据,不能是已经分页的对象
$data = db('table_name')->select();
//$data:未分页的数据 2:每页显示的记录数
$p = new \Page($data,2);
//把分页后的对象$p渲染到模板
$this->assign([
'p' => $p,
]);
return $this->fetch();
使用方法:最后在模板中使用这个分页后的对象$p,通过$p->属性,获取对应的数据
//$p->data; //当前页数据
//$p->render; 分页html模板
//$p->page; //当前第几页
//$p->total; //总记录数
//$p->listRows; //每页显示记录数
//$p->pageNum; //总页数
//根据需要组装数据输出显示,例如:
//遍历数据
{volist name='$p->data' id='vo'}
//code...
{/volist}
//分页html模板输出
//默认已经载入h-ui框架css样式,否则需要重写css样式
{$p->render}
注意:支持URL模式,模块/控制器/操作?参数名=参数值&...
TP5分页类的更多相关文章
- TP5 分页类,自定义样式
结合X-admin 后台框架 在做项目,为了保持分页风格的一致,所以需要自定义 一个分页类. 一.在项目的 extend 目录,创建 cus 目录 二.创建 Page 分页类,代码如下 <?ph ...
- 再看thinkphp5分页类使用
之前使用tp5的分页paginate类时只用到了第一个参数,也就是每页显示多少行 今天又仔细看了下手册和paginate类,发现paginate可传入的参数有很多,可以满足更多需求 比如可以指定分页的 ...
- php实现的分页类
php分页类文件: <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 pr ...
- asp.net的快捷实用分页类
KeleyiPager分页类,可以于对列表页进行分页浏览,代码是从HoverTreeCMS项目中COPY的,感觉很不错,使用简单方便,但是功能强大. 在线体验效果:http://cms.hovertr ...
- php分页类
1.需求 学会php分页类的使用 2.参考例子 CI的分页类 3.代码部分 <?php class pagination{ public $pagesize=20; public $pagein ...
- PHPCMS V9 分页类的修改教程
首先,打开 phpcms\libs\functions\global.func.php 这个文件,找到文件第622行的分页函数,复制一下,粘贴到默认分页函数的下面,重新命名后保存.(笔者在此命名为:p ...
- php 简单分页类
/** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 privat ...
- PHP简单漂亮的分页类
本文介绍一款原生的PHP分页类,分页样式有点类似bootstrap. <?php /* * ********************************************* * @类名 ...
- ThinkPHP 分页类的使用及退出功能的实现
/* ThinkPHP设置编码统一: 一.数据库设置为utf8_bin 二.HTML页面设置charset=utf-8,而且检查文档编码格式是否是utf-8.phpDesigner8设置方式为“文件- ...
随机推荐
- 响应式 Web 设计指南「基础篇」
Web 是普遍存在的,也是无处不在的,Web可以适应任何尺寸的屏幕以及任何使用环境,因为Web有其固有的灵活性和可塑性. Web 再也不是某一平台独有的矿藏,而是真正成为了一张名副其实的大网,并将各种 ...
- 使用HttpClient实现并发请求
在.Net 4.0之前,一直是依靠HttpWebRequest实现Http操作的.它默认有一个非常保守的同一站点下最大2并发数限制,导致默认情况下HttpWebRequest往往得不到理想的速度,必须 ...
- ORACLE中的字符串替换 replce、regexp_replace 和 translate
一.语法 replace(str_source,str1,str2) 把 str_source 中 str1 字符串替换为 str2 字符串,当 str2 为 null 或'' 时,与下个作用相同 ...
- Android开发中遇到的问题(二)——新建android工程的时候eclipse没有生成MainActivity和layout布局
一.新建android工程的时候eclipse没有生成MainActivity和layout布局 最近由于工作上的原因,开始学习Android开发,在入门的时候就遇到了不少的坑,遇到的第一个坑就是&q ...
- 9.5 dubbo事件通知机制
dubbo事件通知机制:http://dubbo.io/books/dubbo-user-book/demos/events-notify.html 一.使用方式 两个服务: DemoService: ...
- C#中流的读写器BinaryReader、BinaryWriter,StreamReader、StreamWriter详解【转】
https://blog.csdn.net/ymnl_gsh/article/details/80723050 C#的FileStream类提供了最原始的字节级上的文件读写功能,但我们习惯于对字符串操 ...
- C# 远程服务器 安装、卸载 Windows 服务,读取远程注册表,关闭杀掉远程进程
这里安装windows服务我们用sc命令,这里需要远程服务器IP,服务名称.显示名称.描述以及执行文件,安装后需要验证服务是否安装成功,验证方法可以直接调用ServiceController来查询服务 ...
- Exception的ToString()方法究竟返回的是什么
最近项目上线后遇到exception没有堆栈信息.所以跟踪一下 源码,其中主要的code如下: // Returns the stack trace as a string. If no stack ...
- android-activity生命周期方法
整个Activity生命周期中的所有方法,我们可以根据程序的需要来覆盖相应的方法: public class Activity extends ApplicationContext { //创建的时候 ...
- 为什么用svg放弃了iconfont?
svg替代iconfont的好处(无论是基于Vue.Jquery),都推荐svg http://www.woshipm.com/pd/463866.html svg图标库,svg图标在线制作 http ...