修改了全部栏目搜索不到数据的bug
1、修改后台添加、修改内容时添加栏目id到搜索表中
找到 phpcms\model\content_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、修改搜索模板文件
找到 phpcms\model\search_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、修改后台更新全站缓存位置
找到phpcms\modules\content\classes\search_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'];
找到phpcms\modules\search\search_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、修改前台搜索程序
找到 phpcms\modules\search\index.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、后台-模块-全站搜索-重建索引部分修改
打开phpcms\modules\search\search_admin.php找到public function createindex()
$datas = $search_api->fulltext_api($pagesize,$page);下面的foreach修改为
foreach ($datas as $id=>$r) { $this->db->update_search($typeid ,$id, $r['fulltextcontent'],$r['title'],$r['adddate'], 1,$r['catid']); }
5、最后一步,到数据库中,找到数据表 表前缀_search,在此表中加一字段 catid int 4 做个索引,
OK,去按栏目id搜索试试吧,可以了,调试可用。
6、因为有的朋友不知道怎么调用,所以简单写了个调用的表单
{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>
相关文章
phpcms-v9中将一个站点下的文章内容完美的推送到另一个站点下2024-09-24
phpcms loop循环判断是否为最后一条的代码2024-06-18
Phpcms V9 文章或图片模型增加相关下载功能2022-12-30
phpcms文章列表增加一键推送到百度功能2022-12-08
phpcms 标题SEO栏目不同的代码2022-11-14
PHPCMS替换内容的两种方法:str_replace和preg_replace2022-10-25
phpcms 搜索结果第一页比设定的多一条数据的解决办法2021-01-05
phpcms v9 字段多行文本不换行的解决办法2020-09-22
phpcms 栏目和文章页面“您没有访问该信息的权限”跳转登陆页面2020-09-17
phpcms 模板header包含action="lists"导致搜索结果错误的解决方案2020-04-26