thinkphp框架page类与bootstrap分页(美化)


Posted in PHP onJune 25, 2017

bootstrap分样式使用方法这里写链接内容

<nav aria-label="Page navigation">
 <ul class="pagination">
  <li>
   <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" aria-label="Previous">
    <span aria-hidden="true">«</span>
   </a>
  </li>
  <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >1</a></li>
  <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >2</a></li>
  <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >3</a></li>
  <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >4</a></li>
  <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >5</a></li>
  <li>
   <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" aria-label="Next">
    <span aria-hidden="true">»</span>
   </a>
  </li>
 </ul>
</nav>

1.找到Thinkphp中的Page.class.php,然后使用下面的文件内容完全替换

<?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  = 11;// 分页栏每页显示的页数
  public $lastSuffix = true; // 最后一页是否显示总页数
  private $p    = 'p'; //分页参数名
  private $url   = ''; //当前链接URL
  private $nowPage = 1;
  // 分页显示定制
  private $config = array(
    'header' => '<li><span>共 %TOTAL_ROW% 条记录<span class="sr-only"></span></span></li>',
    'prev'  => '<<',
    'next'  => '>>',
    'first' => '1...',
    'last'  => '...%TOTAL_PAGE%',
    'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',
  );
  /**
   * 架构函数
   * @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(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);
    $this->lastSuffix && $this->config['last'] = $this->totalPages;
    //上一页
    $up_row = $this->nowPage - 1;
    $up_page = $up_row > 0 ? '<li><a class="prev" href="' . $this->url($up_row) . '" rel="external nofollow" >' . $this->config['prev'] . '</a></li>' : '';
    //下一页
    $down_row = $this->nowPage + 1;
    $down_page = ($down_row <= $this->totalPages) ? '<li><a class="next" href="' . $this->url($down_row) . '" rel="external nofollow" >' . $this->config['next'] . '</a></li>' : '';
    //第一页
    $the_first = '';
    if($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1){
      $the_first = '<li><a class="first" href="' . $this->url(1) . '" rel="external nofollow" >' . $this->config['first'] . '</a></li>';
    }
    //最后一页
    $the_end = '';
    if($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages){
      $the_end = '<li><a class="end" href="' . $this->url($this->totalPages) . '" rel="external nofollow" >' . $this->config['last'] . '</a></li>';
    }
    //数字连接
    $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 && $page != $this->nowPage){
        if($page <= $this->totalPages){
          $link_page .= '<li><a class="num" href="' . $this->url($page) . '" rel="external nofollow" >' . $page . '</a></li>';
        }else{
          break;
        }
      }else{
        if($page > 0 && $this->totalPages != 1){
          $link_page .= '<li class="active "><span>'.$page.'<span class="sr-only"></span></span></li>';
        }
      }
    }
    //替换分页内容
    $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 "<ul class='pagination'>{$page_str}</ul>";
  }
}

2.相关控制器代码

//所有新闻
  public function all_news(){
    $Article=M("Article");
    $where['article_type']=1;
    //查询满足要求的总的记录数
    $count=$Article->where($where)->count();
    //实例化分页类传入总记录数和煤业显示的记录数
    $Page=new \Think\Page($count,1);
    //分页显示输出
    $show=$Page->show();
    // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
    $news=$Article->where($where)->order('pub_time')->field('id,title,institution_type,author_name,pub_time')->limit($Page->firstRow.','.$Page->listRows)->select();
    //赋值数据集
    $this->assign('news',$news);
    //赋值分页输出
    $this->assign('page',$show);
    $this->display();
  }

3.html中只需要

<div class="panel-body center">
           {$page}

以上所述是小编给大家介绍的thinkphp框架page类与bootstrap分页(美化),希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

PHP 相关文章推荐
PHP Directory 函数的详解
Mar 07 PHP
PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程
Jun 12 PHP
初识Laravel
Oct 30 PHP
php自定义加密与解密程序实例
Dec 31 PHP
PHP获取指定月份第一天和最后一天的方法
Jul 18 PHP
php实现多城市切换特效
Aug 09 PHP
php使用pear_smtp发送邮件
Apr 15 PHP
利用Homestead快速运行一个Laravel项目的方法详解
Nov 14 PHP
PHP抽象类与接口的区别详解
Mar 21 PHP
php解决约瑟夫环算法实例分析
Sep 30 PHP
php面向对象基础详解【星际争霸游戏案例】
Jan 23 PHP
PHP如何使用JWT做Api接口身份认证的实现
Feb 03 PHP
解决出现SoapFault (looks like we got no XML document)的问题
Jun 24 #PHP
php-fpm开启状态统计的方法详解
Jun 23 #PHP
PHP多种序列化/反序列化的方法详解
Jun 23 #PHP
PHP后端银联支付及退款实例代码
Jun 23 #PHP
PHP单例模式简单用法示例
Jun 23 #PHP
php处理静态页面:页面设置缓存时间实例
Jun 22 #PHP
PHP使用redis消息队列发布微博的方法示例
Jun 22 #PHP
You might like
php输出表格的实现代码(修正版)
2010/12/29 PHP
php实现发送微信模板消息的方法
2015/03/07 PHP
Javascript打印网页部分内容的脚本
2008/11/17 Javascript
Javascript select控件操作大全(新增、修改、删除、选中、清空、判断存在等)
2008/12/19 Javascript
jQuery 开天辟地入门篇一
2009/12/09 Javascript
jQuery选择器中含有空格的使用示例及注意事项
2013/08/25 Javascript
JavaScript组合拼接字符串的效率对比测试
2014/11/06 Javascript
浅析javascript 定时器
2014/12/23 Javascript
轻松学习jQuery插件EasyUI EasyUI实现拖放商品放置购物车
2015/11/30 Javascript
使用plupload自定义参数实现多文件上传
2016/07/19 Javascript
JS中的三个循环小结
2017/06/20 Javascript
javascript实现的字符串转换成数组操作示例
2019/06/13 Javascript
vuex实现购物车功能
2020/06/28 Javascript
微信小程序实现多张图片上传功能
2020/11/18 Javascript
[01:07:53]RNG vs VG 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
利用Python中SocketServer 实现客户端与服务器间非阻塞通信
2016/12/15 Python
python实现定时自动备份文件到其他主机的实例代码
2018/02/23 Python
python读写LMDB文件的方法
2018/07/02 Python
学生信息管理系统Python面向对象版
2019/01/30 Python
python获取微信企业号打卡数据并生成windows计划任务
2019/04/30 Python
Python3+PyInstall+Sciter解决报错缺少dll、html等文件问题
2019/07/15 Python
使用 Python 合并多个格式一致的 Excel 文件(推荐)
2019/12/09 Python
python中如何使用insert函数
2020/01/09 Python
解决tensorboard多个events文件显示紊乱的问题
2020/02/15 Python
Python 3.10 的首个 PEP 诞生,内置类型 zip() 迎来新特性(推荐)
2020/07/03 Python
HTML5实现的震撼3D焦点图动画的示例代码
2019/09/26 HTML / CSS
双立人加拿大官网:Zwilling加拿大
2020/08/10 全球购物
音乐专业自荐信
2014/02/07 职场文书
体育口号大全
2014/06/18 职场文书
市场营销工作计划书
2014/09/15 职场文书
初婚初育证明范本
2014/11/24 职场文书
干部考察材料范文
2014/12/24 职场文书
文体活动总结
2015/02/04 职场文书
运动会口号霸气押韵
2015/12/24 职场文书
HTML中实现音乐或视频自动播放案例详解
2022/05/30 HTML / CSS
Go微服务项目配置文件的定义和读取示例详解
2022/06/21 Golang