小伙伴们若是想做下图所示的高级搜索功能该怎么做呢?
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>
关键字:
<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();
}