欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

TP3.2框架分页相关实现方法分析

程序员文章站 2022-04-10 08:46:02
本文实例讲述了tp3.2框架分页相关实现方法。分享给大家供大家参考,具体如下:需要用到分页的继承这个控制器即可

本文实例讲述了tp3.2框架分页相关实现方法。分享给大家供大家参考,具体如下:

需要用到分页的继承这个控制器即可

<?php
namespace home\controller;
use think\controller;
class basecontroller extends controller {
    /**
     * 基础分页的相同代码封装,使前台的代码更少
     * @param $count 要分页的总记录数
     * @param int $pagesize 每页查询条数
     * @return \think\page
    */
    function getpage($count, $pagesize = 20,$waps) {
          $page = new \think\page($count,$pagesize,$waps);
     $page->rollpage = 5;
     $page->setconfig('header', '<li class="rows">第<b>%now_page%</b>页/共<b>%total_page%</b>页</li>');
       $page->setconfig('prev', '上一页');
       $page->setconfig('next', '下一页');
     $page->setconfig('last', '末页');
         $page->setconfig('first', '首页');
         $page->setconfig('theme', '%first%%up_page%%link_page%%down_page%%end%%header%');
         $page->lastsuffix = false;//最后一页不显示为总页数
         return $page;
    }
    /**
     * @param $model     模型
     * @param $map         where条件
     * @param $find      0 select 1=find 2=关联表
     * @return $list     数组
    */
    function _select($model,$map,$order='',$field="",$find="0",$limit=20,$join,$param){
        $waps = $param;
        if(empty($model) && empty($map)){
            $this->list = array();
        }else{
            if($find == 0){
                $count = $model->where($map)->count();
                $page = $this->getpage($count,$limit,$waps);
                if($page->firstrow < 0){
                    $page->firstrow = 0;
                }
                if($order != ""){
                    $this->list = $model->field($field)->where($map)->order($order)->limit($page->firstrow. ',' . $page->listrows)->select();
                }else{
                    $this->list = $model->field($field)->where($map)->limit($page->firstrow. ',' . $page->listrows)->select();    
                }
                $this->page = $page->show();
            }else if($find == 1){
                $this->list = $model->field($field)->where($map)->find();
            }else if($find == 3){
                $count = $model->alias("t1")->join($join)->where($map)->count(); 
                $page = $this->getpage($count,$limit,$waps);
                if($page->firstrow < 0){
                    $page->firstrow = 0;
                }
                if($order != ""){
                    $this->list = $model->alias("t1")->join($join)->field($field)->where($map)->order($order)->limit($page->firstrow. ',' . $page->listrows)->select();
                }else{
                    $this->list = $model->alias("t1")->join($join)->field($field)->where($map)->limit($page->firstrow. ',' . $page->listrows)->select();    
                }
                $this->page = $page->show();
            }else if($find == 4){
                if($order != ""){
                    $this->list = $model->alias("t1")->join($join)->field($field)->where($map)->order($order)->limit($page->firstrow. ',' . $page->listrows)->find();
                }else{
                    $this->list = $model->alias("t1")->join($join)->field($field)->where($map)->limit($page->firstrow. ',' . $page->listrows)->find();    
                }
            }
            $this->limit = $page->firstrow;
            $this->assign("listinfo",$this->list);
            $this->assign("page",$this->page);
            return($this->list);
        }
    }
}

控制器中先继承后使用。

if($this->txt_keyword){
    $where['m_title'] = array('like','%'.$this->txt_keyword.'%');
}
 
$where['status'] = $param['status'] = 0;
$this->_select($this->information_mod,$where,'','*',0,10,'',$param);
$this->display();

模板中

<div class="hg_page" >
   {$page}
</div>

分页样式

<style>
 .hg_page .first, .hg_page .current, .hg_page .num, .hg_page .next, .hg_page .end, .hg_page .rows, .hg_page .prev{
  float: left;
  height: 37px;
  padding: 0 15px;
  border: 1px solid #e5e5e5;
  line-height: 37px;
  margin: 0 3px;
 }
 .hg_page{
  text-align: center;
  height: 37px;
  margin:0 auto;
  margin-top: 30px;
  margin-bottom: 20px;
  width: 800px;
 }
</style>

相关标签: TP3.2 分页