最近有一笔单子是需要将搜索的功能二次开发,实现搜索按照后台添加的栏目进行搜索,仿站网花了半天功夫,改了下源码,实现了这个功能,今天把修改的方法贴给大家,有需要的朋友可以看看
1、修改后台添加、修改内容时添加栏目id到搜索表中
找到 phpcmsmodelcontent_model.class.php 大概106、287行左右,一共两处
$this->search_api($id,$inputinfo);
修改为:
$this->search_api($id,$systeminfo['catid'],$inputinfo);
找到 大概381行,将private function search_api 方法替换为以下代码
private function search_api($id = 0,$catid, $data = array(), $action = 'update') {
$type_arr = getcache('search_model_'.$this->siteid,'search');
$typeid = $type_arr[$this->modelid]['typeid'];
if($action == 'update') {
$fulltext_array = getcache('model_field_'.$this->modelid,'model');
foreach($fulltext_array AS $key=>$value){
if($value['isfulltext']) {
$fulltextcontent .= $data['system'][$key] ? $data['system'][$key] : $data['model'][$key];
}
}
$this->search_db->update_search($typeid ,$id, $fulltextcontent,addslashes($data['system']['title']).' '.addslashes($data['system']['keywords']),$data['system']['inputtime'],'',$catid);
} elseif($action == 'delete') {
$this->search_db->delete_search($typeid ,$id);
}
}
2、修改搜索模板文件
找到 phpcmsmodelsearch_model.calss.php 的 21行左右
将 update_search 方法替换为:
public function update_search($typeid ,$id = 0,$data = '',$text = '',$adddate = 0, $iscreateindex=0,$catid) {
$segment = pc_base::load_sys_class('segment');
//分词结果
$fulltext_data = $segment->get_keyword($segment->split_result($data));
$fulltext_data = $text.' '.$fulltext_data;
if(!$iscreateindex) {
$r = $this->get_one(array('typeid'=>$typeid,'id'=>$id),'searchid');
}
if($r) {
$searchid = $r['searchid'];
$this->update(array('data'=>$fulltext_data,'adddate'=>$adddate,'catid'=>$catid),array('typeid'=>$typeid,'id'=>$id));
} else {
$siteid = param::get_cookie('siteid');
$searchid = $this->insert(array('typeid'=>$typeid,'id'=>$id,'adddate'=>$adddate,'data'=>$fulltext_data,'siteid'=>$siteid,'catid'=>$catid),true);
}
return $searchid;
}
3、修改后台更新全站缓存位置
找到phpcmsmodelscontentclassessearch_api.class.php 的第 31行
将 $system_keys = 'id,inputtime'.implode(',',$system_keys);
修改为
$system_keys = 'id,inputtime,catid,'.implode(',',$system_keys);
找到 $temp['adddate'] = $r['inputtime'];在后面加上
$temp['catid'] = $r['catid'];
找到phpcmsmodelssearchsearch_admin.php大概第85行,将foreach修改为下面
foreach ($datas as $id=>$r) {
$this->db->update_search($typeid ,$id, $r['fulltextcontent'],$r['title'],$r['adddate'], 1,$r['catid']);
}
4、修改前台搜索程序
找到 phpcmsmodelssearchindex.php 第25行
在 if(isset($_GET['q'])) { 下加上
$catid =$_GET['catid']?" AND catid = '$_GET[catid]'":'';
然后在第57行左右,将
$commend = $this->db->get_one("`typeid` = '$typeid' $sql_time AND `data` like '%$q%'");
修改为
$commend = $this->db->get_one("`typeid` = '$typeid' $sql_time $catid AND `data` like '%$q%'");
再找到第84行左右将
//如果分词结果为空
if(!empty($segment_q)) {
$sql = "`siteid`= '$siteid' AND `typeid` = '$typeid' $sql_time AND MATCH (`data`) AGAINST ('$segment_q' IN BOOLEAN MODE)";
} else {
$sql = "`siteid`= '$siteid' AND `typeid` = '$typeid' $sql_time AND `data` like '%$q%'";
}
修改为
//如果分词结果为空
if(!empty($segment_q)) {
$sql = "`siteid`= '$siteid' AND `typeid` = '$typeid' $catid $sql_time AND MATCH (`data`) AGAINST ('$segment_q' IN BOOLEAN MODE)";
} else {
$sql = "`siteid`= '$siteid' AND `typeid` = '$typeid' $catid $sql_time AND `data` like '%$q%'";
}
5、最后一步,进入数据库,找到数据表 v9_search,在此表中加一字段 catid int 4 做个索引,这样就完成了,前台调用的代码为:
{php $cat_id = 6;}<!--此处是一级栏目id,可以自己设置-->
<form name="search" id="" action="" method="get">
<li>关 键 词 :<input name="q" value="请输入查询关键词" type="text" onclick="javascript:document.getElementById('q').value='';" id="q" style="width:150px;"/></li>
<li>产品类别:<select name="catid" style=" width:150px;">
<option value="{$cat_id}">所有产品分类</option>
{loop subcat($cat_id) $v}
{php if($v['type']!=0) continue;}
<option value="{$v[catid]}">{$v[catname]}</option>
{/loop}
</select></li>
<input type="hidden" value="search" name="m">
<input type="hidden" value="index" name="c">
<input type="hidden" value="init" name="a">
<input id="typeid" type="hidden" value="1" name="typeid">
<input id="siteid" type="hidden" value="1" name="siteid">
<li><input type="submit" name="submit" id="submit" value=""></li>
</form>
文章由仿站网原创,转载请带上出处链接