tp3.2分页

使用tp3.2自带的分页类

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Think; class Page{
public $firstRow; // 起始行数
public $listRows; // 列表每页显示行数
public $parameter; // 分页跳转时要带的参数
public $totalRows; // 总行数
public $totalPages; // 分页总页面数
public $rollPage = 5;// 分页栏每页显示的页数
public $lastSuffix = true; // 最后一页是否显示总页数
private $p = 'p'; //分页参数名
private $url = ''; //当前链接URL
private $nowPage = 1;
// 分页显示定制
private $config = array(
'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>',
'first' => '首页',
'prev' => '上一页',
'next' => '下一页',
'last' => '末页',
'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%',
);
/**
* 架构函数
* @param array $totalRows 总的记录数
* @param array $listRows 每页显示记录数
* @param array $parameter 分页跳转的参数
*/
public function __construct($totalRows, $listRows=20, $parameter = array()) {
C('VAR_PAGE') && $this->p = C('VAR_PAGE'); //设置分页参数名称
/* 基础设置 */
$this->totalRows = $totalRows; //设置总记录数
$this->listRows = $listRows; //设置每页显示行数
$this->parameter = empty($parameter) ? $_GET : $parameter;
$this->nowPage = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);
$this->nowPage = $this->nowPage>0 ? $this->nowPage : 1;
$this->firstRow = $this->listRows * ($this->nowPage - 1);
}
/**
* 定制分页链接设置
* @param string $name 设置名称
* @param string $value 设置值
*/
public function setConfig($name,$value) {
if(isset($this->config[$name])) {
$this->config[$name] = $value;
}
}
/**
* 生成链接URL
* @param integer $page 页码
* @return string
*/
private function url($page){
return str_replace(urlencode('[PAGE]'), $page, $this->url);
}
/**
* 组装分页链接
* @return string
*/
public function show() {
if(0 == $this->totalRows) return '';
/* 生成URL */
$this->parameter[$this->p] = '[PAGE]';
$this->url = U(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME, $this->parameter);
/* 计算分页信息 */
$this->totalPages = ceil($this->totalRows / $this->listRows); //总页数
if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
$this->nowPage = $this->totalPages;
}
/* 计算分页零时变量 */
$now_cool_page = $this->rollPage/2;
$now_cool_page_ceil = ceil($now_cool_page);
//上一页
$up_row = $this->nowPage - 1;
$up_page = $up_row > 0 ? '<a class="prev" href="' . $this->url($up_row) . '">' . $this->config['prev'] . '</a>' : '<a class="prev not-allowed" href="javascript:;">' . $this->config['prev'] . '</a>';
//下一页
$down_row = $this->nowPage + 1;
$down_page = ($down_row <= $this->totalPages) ? '<a class="next" href="' . $this->url($down_row) . '">' . $this->config['next'] . '</a>' : '<a class="next not-allowed" href="javascript:;">' . $this->config['next'] . '</a>';
//第一页
$the_first = '<a class="first" href="' . $this->url(1) . '">' . $this->config['first'] . '</a>';
//最后一页
$the_end = '<a class="end" href="' . $this->url($this->totalPages) . '">' . $this->config['last'] . '</a>';
//数字连接
$link_page = "";
for($i = 1; $i <= $this->rollPage; $i++){
if(($this->nowPage - $now_cool_page) <= 0 ){
$page = $i;
}elseif(($this->nowPage + $now_cool_page - 1) >= $this->totalPages){
$page = $this->totalPages - $this->rollPage + $i;
}else{
$page = $this->nowPage - $now_cool_page_ceil + $i;
}
if ($page>0) {
if($page != $this->nowPage){
if($page <= $this->totalPages){
$link_page .= '<a class="num" href="' . $this->url($page) . '">' . $page . '</a>';
}else{
break;
}
}else{
$link_page .= '<span class="current">' . $page . '</span>';
}
}
}
//替换分页内容
$page_str = str_replace(
array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),
array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $this->totalRows, $this->totalPages),
$this->config['theme']);
return '<div class="page">'.$page_str.'</div>';
}
}

前端页面

<div class="pagelink">
<ul class="paginList pager"> {$show}
</ul>
</div>

样式

 <style type="text/css">
.b-page {
background: #fff;
box-shadow: 0px 1px 2px 0px #E2E2E2;
}
.page {
width: 100%;
padding: 30px 15px;
background: #FFF;
text-align: center;
overflow: hidden;
}
.page .first,
.page .prev,
.page .current,
.page .num,
.page .current,
.page .next,
.page .end {
padding: 8px 16px;
margin: 0px 5px;
display: inline-block;
color: #008CBA;
border: 1px solid #F2F2F2;
border-radius: 5px;
}
.page .first:hover,
.page .prev:hover,
.page .current:hover,
.page .num:hover,
.page .current:hover,
.page .next:hover,
.page .end:hover {
text-decoration: none;
background: #F8F5F5;
}
.page .current {
background-color: #008CBA;
color: #FFF;
border-radius: 5px;
border: 1px solid #008CBA;
}
.page .current:hover {
text-decoration: none;
background: #008CBA;
}
.page .not-allowed {
cursor: not-allowed;
}
</style>

后台调用

 $User = M('api_article'); // 实例化User对象
$count = $User->where(array('er_id'=>$id))->count();// 查询满足要求的总记录数
$Page = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(25)
$show = $Page->show();// 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
$article = $User->where(array('er_id'=>$id))->order('fabutime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
$this->assign('show',$show);// 赋值分页输出
       $this->assign('article', $article);//二级分类文章列表       

           $this->display();

tp3.2分页的更多相关文章

  1. tp3.2分页功能

    后台 1.利用Page类和limit方法分页 $User = M('User'); // 实例化User对象 $count = $User->where('status=1')->coun ...

  2. 【TP3.2+Oracle】数据进行分页

    1.写在前面:mysql的分页 通过limit 关键字进行处理, oracle却没有limit,而是用ROWNUM 字段来进行分页 2.参考示例,TP3.2 代码,其实原理看懂了 其他框架和原生都可以 ...

  3. 【TP3.2】TP3.2下实现ajax分页(原创+亲测可用)

    一,写在最开始:ajax分页的原理,是利用了js的ajax执行请求,获取分页list和分页page [代码块],去替换页面显示数据的[代码块] 技术:js的ajax + TP3.2的fetch(&qu ...

  4. TP3.2写分页

    用TP3.2写分页 手册上说的好难懂,我自己去网上找资料 ,现在整理一下,以后可能会用: 在Think下面有Page.class.php类: 我在这个下面放了一个function.php的(算是类吧又 ...

  5. TP3.23 与Laypage 结合进行分页

    demo地址:http://tp.ytlwin.top 控制器 <?php namespace Home\Controller; use Think\Controller; class Inde ...

  6. 用layer插件实现tp3.2的分页

    主要需要用到  /layer/layer.js  这个, 现在一个tp前端视图/article/index.html <!DOCTYPE html> <html lang=" ...

  7. 【TP3.2 + 其他任何PHP框架】编辑、删除、添加数据,返回原分页 (ajax+form两种方式提交均可以)

    1.目的1:在如下的一个页面中,p=2,比如我们删除数据id=13,通过ajax提交{id,p} 这2个参数,就可以了,页面返回json的url参数中原样带上p即可. 2.目的2: 步骤1:在如下页面 ...

  8. tp3 key json 分页

    //json 强制转换为array $arr[$key]['checkpro'] = json_decode($val['checkpro'],JSON_FORCE_ARRAY);    $arr[$ ...

  9. simple_tag,filte,分页以及cookie和装饰器

    自定义simple_tag 内置的方法 首先Django中包含了很多内置的方法: 这里通过lower实现 在views视图函数中写如下代码: def tp3(request): name= " ...

随机推荐

  1. KiCad 如何画板框

    KiCad 如何画板框 一块 PCB 最开始就是画板框. 设置工作到 Edge.Cuts 点菜单放置 -> 线(L) 板框画好.

  2. Flex Cairngorm框架知识整理

    简介: Cairngorm是一个开源的Flex项目,为FLex提供了一个类似MVC的体系结构框架,它是Flex RIA开发的最好框架之一.使用Cairngorm框架可以大大提高开发和维护的效率. Ca ...

  3. 使用Jenkins 安装和自动化部署项目

    安装 jenkins 安装方法可以使用war 安装 或者服务安装.我这里使用  rpm 安装 第一步: 我们使用的 centos  ,选着这个,直接 下载传到服务器上.或者 复制下载链接 到 服务器上 ...

  4. 解决The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone

    Spring Boot JPA 使用Mysql是出现如下错误: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represe ...

  5. Spark Hadoop Free 安装遇到的问题

    运行 ./sbin/start-master.sh : SparkCommand:/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java -cp /home/se ...

  6. spring4与mongodb的集成

    新项目的辅助系统,需要用到mongo系统,今天再次将其使用环境进行了操作搭建.还是遇到一些问题,毕竟之前使用的场景和现在的不同.版本也不一样了. 本次使用的环境: mongo:3.4.4版本 OS: ...

  7. DOM confirm setTimeout url刷新

    console.log 输出框 alert 弹出框 confirm 确认框 // URL和刷新 location.href 获取URL location.href = "url" ...

  8. Ubuntu 14.10 下Eclipse安装Hadoop插件

    准备环境 1 安装好了Hadoop,之前安装了Hadoop 2.5.0,安装参考http://www.cnblogs.com/liuchangchun/p/4097286.html 2 安装Eclip ...

  9. 51nod 1132 覆盖数字的数量 V2

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1132 题意是给定a,b,l,r求[l,r]内有几个整数可以表示成ax+b ...

  10. react组件的数据传递

    在react中,为了解决html标签构建应用的不足,将公共的功能单独抽离成一个文件作为一个组件,在使用的地方按需引入,既然是组件彼此调用,就会涉及到父子组件的通信,下面主要来总结简单的组件通信. 1, ...