当前位置:首页 > PHP教程 > PHP常见问题

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

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 thinkpage($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分页(美化),希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!