ThinkPHP3.2框架自带分页功能实现方法示例

时间:2022-07-27
本文章向大家介绍ThinkPHP3.2框架自带分页功能实现方法示例,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

本文实例讲述了ThinkPHP3.2框架自带分页功能实现方法。分享给大家供大家参考,具体如下:

1.前端-分页代码:

<tfoot 
<!--分页显示?-- 
<tr 
  <td textalign="center" cl nowrap="true" colspan="9" height="20" 
   <div class="pages" {$page}</div 
  </td 
</tr 
</tfoot 

2.创建分页样式:如page.css 并将以下代码复制到该文件中

.pages{float: right}
.pages a,.pages span {
  display:inline-block;
  padding:2px 10px;
  border:1px solid #f0f0f0;
  -webkit-border-radius:3px;
  -moz-border-radius:3px;
  border-radius:3px;
  font-size: 14px;
}
.pages a,.pages li {
  display:inline-block;
  list-style: none;
  text-decoration:none; color:#58A0D3;
}
.pages a.first,.pages a.prev,.pages a.next,.pages a.end{
  margin:0 auto;
}
.pages a:hover{
  border-color:#50A8E6;
}
.pages span.current{
  background:#50A8E6;
  color:#FFF;
  font-weight:700;
  border-color:#50A8E6;
}

3.前端页面引入分页样式css文件

4.控制器中编写index方法,将数据显示到模板

方法(一):利用Page类和limit方法分页

<?php
namespace AdminController;
use ThinkController;
class DocController extends Controller{
  function index(){
    //实例化Doc数据表模型
    $doc = D('Doc');
    //调用count方法查询要显示的数据总记录数
    $count = $doc- count();
    //echo $count;die;
    $page = new ThinkPage($count,2);
    // 分页显示输出
    $show = $page- show();
    $this- assign('page',$show);
    // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
    $doc_list = $doc- limit($page- firstRow.','.$page- listRows)- select();
    $this- assign('doc_list',$doc_list);
    $this- display();
  }

方法(二):分页类和page方法的实现分页

<?php
namespace AdminController;
use ThinkController;
class DocController extends Controller{
  function index(){
    //实例化Doc数据表模型
    $doc = D('Doc');
    //进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
    $doc_list = $doc- page($_GET['p'] . ',2')- select();
    $this- assign('doc_list', $doc_list);// 赋值数据集
    $count = $doc- count();// 查询满足要求的总记录数
    $page = new ThinkPage($count, 2);// 实例化分页类 传入总记录数和每页显示的记录数
    $show = $page- show();// 分页显示输出
    $this- assign('page', $show);// 赋值分页输出
    $this- display(); // 输出模板
  }