实现效果:
前端html代码:
<input type="text" class="form-control" name="keyword" placeholder="请输入关键词" value='{$keyword}' id="keyword"/> <img src="__HOME__/img/search.png" alt="" class='searchBtn'/>
前端script代码:
<script> $(function(){ $('.searchBtn').click(function(){ var keyword = $('#keyword').val(); if(!keyword){ alert("请输入搜索词") return false; } window.location.href="/product?keyword=" + keyword; //搜索内容跳转页面 }) }) </script>
后端代码:
//当前分类id $cate_id = $child_id ? $child_id : $cate_id; $current_cate = M("Productcate")->where("id=$cate_id")->find(); //M实例化一个没有模型文件的Model $this->assign('current_cate', $current_cate); $c_ids = array(); $children = D('Productcate')->catList($cate_id, '', false); //查找当前分类的所有子类 foreach ($children as $k => $v) { $c_ids[] = $v['id']; } $where = ''; if ($keyword = I('keyword')) { $where = " and title like '%{$keyword}%' ";//模糊查询标题 $this->assign('keyword', $keyword); } $pro_list = $this->lists('Product', " status = 1 and ((cate_id in(" . implode(',', $c_ids) . ") ) $where )", 'sort asc,id desc');
$this->assign('list', $pro_list);
成功的秘诀,在永不改变既定的目的~