TP5.0 分类搜索--解决分页后搜索条件缺失问题

小伙伴们若是想做下图所示的高级搜索功能该怎么做呢?

 

HTML层代码

<form class="well form-inline margin-top-20" method="where" action="{:url('AdminClass/index')}">
        分类:
        <select class="form-control" name="category" style="width: 160px;">
            <option value='0'>全部</option>
            <volist name="category" id="category">
                <option value="{$category['t_id']}" <if condition="$type_id eq $category.t_id">selected="selected"</if> >{$category['t_post_title']}</option>
            </volist>
        </select> &nbsp;&nbsp;
       
        关键字:
        <input type="text" class="form-control" name="keyword" style="width: 200px;"
               value="{$keyword|default=''}" placeholder="标题名称...">
        <input type="submit" class="btn btn-primary" value="搜索"/>
    </form>

 

控制器层代码

public function index(){

        // 接收搜索的信息 $param['category']、$param['keyword']
        $param = $this->request->param();

        $condition['status'] = 1;
        if(!empty($param)){
            if($param['category'] != 0){
                $condition['type_id'] = $param['category'];
            }
            if(trim($param['keyword']) != ''){
                $condition['post_title'] = array('like','%'.$param['keyword'].'%');
            }
        }

        $search = ['query'=>[]];
        $search['query']['keyword'] = isset($param['keyword']) ? $param['keyword'] : '';
        $search['query']['category'] = isset($param['category']) ? $param['category'] : 0;

        $PortalClassTypeModel = new PortalClassTypeModel();
        $PortalClassArticleModel = new PortalClassArticleModel();
        $category = $PortalClassTypeModel->getAll();
        $list = $PortalClassArticleModel->getList($condition,$search);

        $this->assign('category',$category);
        $this->assign('list',$list);
        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
        $this->assign('type_id',isset($param['category'])?$param['category']:0);
        $this->assign('page', $list->render());
        return $this->fetch();
    }

Model 层代码

// 获取文章列表
    public function getList($condition,$search){

        return Db::name("portal_class_article")
               ->alias('a')
               ->join('portal_class_type b','a.type_id = b.t_id')
               ->field('a.*,b.t_post_title')
               ->where($condition)
               ->order('a.create_time DESC')
               ->paginate(10,false,$search);
    }


// 获取全部列表 无分页
    public function getAll(){
        return $this->order('list_order ASC,create_time','DESC')->select();
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值