🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 搜索源码解析 Home/c/HomeController.php > 单模搜索是 search() 方法 > 多模搜索是 searchAll() 方法 > 相关视频教程:[ 极致CMS二次开发高级教程(1)](https://www.bilibili.com/video/av77138645/) ``` //搜索--单一模块搜索 function search(){ $tables = explode('|',$this->webconf['search_table']); $molds = $this->frparam('molds',1);//搜索的模块 $tid = $this->frparam('tid',1); //获取搜索栏目id if(in_array($molds,$tables) && $molds!=''){ $word = $this->frparam('word',1); if($word==''){ //检查是否为ajax提交,返回json格式数据 if($this->frparam('ajax')){ JsonReturn(['code'=>1,'data'=>'','msg'=>'请输入关键词搜索!']); } Error('请输入关键词搜索!'); } $this->word = $word; //进行title模糊搜索,如果要新增搜索字段,比如: //$sql=" isshow=1 "; //$sql.=" and ( title like '%".$word."%' or body like '%".$word."%' )";模糊搜索title //替换下面这一行 $sql = " isshow=1 and title like '%".$word."%' "; if($tid){ $sql.=' and tid in('.$tid.') '; //搜索tid的范围 } $page = new Page($molds); $page->typeurl = 'search'; $data = $page->where($sql)->orderby('id desc')->limit(15)->page($this->frparam('page',0,1))->go(); $pages = $page->pageList(3,'&page='); $this->pages = $pages;//组合分页 foreach($data as $k=>$v){ if(isset($v['htmlurl']) && !isset($v['url'])){ $data[$k]['url'] = gourl($v['id'],$v['htmlurl']); } $data[$k]['title'] = str_replace($word,'<b style="color:#f00">'.$word.'</b>',$v['title']); } $this->lists = $data;//列表数据 $this->sum = $page->sum;//总数据 $this->listpage = $page->listpage;//分页数组-自定义分页可用 $this->prevpage = $page->prevpage;//上一页 $this->nextpage = $page->nextpage;//下一页 $this->allpage = $page->allpage;//总页数 //检查是否ajax提交,返回json数据 if($this->frparam('ajax')){ //检查是否获取ajax模板,此处为手机端便捷操作 /** 可以直接在ajax_search_list.html页面里写输出 同时支持模板标签,比如:foreach循环 参考源码自带的官方demo,手机模式调用内容 **/ if($this->frparam('ajax_tpl')){ $this->display($this->template.'/ajax_search_list'); exit; } JsonReturn(['code'=>0,'data'=>$data,'msg'=>'success']); } $this->display($this->template.'/search'); }else{ if($this->frparam('ajax')){ JsonReturn(['code'=>1,'data'=>'','msg'=>'搜索超出设定范围!']); } Error('搜索超出设定范围!'); } } ``` > 从上面源码解析中,我们可以得出结论 > 缺省参数:ajax=1 即为ajax提交 > 缺省参数:ajax_tpl=1 即为获取ajax模板内容(ajax_search_list.html页面) > 增加搜索字段范围如: > 同时搜索 `title` 和 `body` > >$sql = " isshow=1 "; > > $sql .= " and (title like '%".$word."%' or body like '%".$word."%' ";