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

2022-04-15 0 357

本文实例讲述了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>

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 PHP编程 TP3.2框架分页相关实现方法分析 https://www.niceym.com/15528.html