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\models\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\models\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\models\search\index.php 第25行
在 if(isset($_GET['q'])) { 下加上
$catid =$_GET['catid']?" AND catid in ('$_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、最后一步,到数据库中,找到数据表 表前缀_search,在此表中加一字段 catid int 4 做个索引。
6、模板代码:
{php $cat_id = 11;}
{if $CATEGORYS[$cat_id][child]=='1'}
{php $arrparent_id=str_replace($catid.",","",$CATEGORYS[$catid][arrchildid]);}
{else}
{php $arrparent_id=$CATEGORYS[$cat_id][arrchildid]}
{/if}
<form name="search" id="" action="{APP_PATH}index.php" method="get" target="_blank">
<input type="text" class="text" onclick="javascript:document.getElementById('q').value='';" name="q" id="q" /><input type="submit" value="搜索词条" class="button" />
<input type="hidden" name="m" value="search"/>
<input type="hidden" name="c" value="index"/>
<input type="hidden" name="a" value="init"/>
<input type="hidden" name="typeid" value="1" id="typeid"/>
<input type="hidden" name="catid" value="{$arrparent_id}" id="catid" />
<input type="hidden" name="siteid" value="{$siteid}" id="siteid"/>
</form>
相关文章
PHPCMS 统计栏目下子栏目数量的代码2024-11-12
PHPCMS V9 上传图片或附件后后台退出需要重新登录的解决办法2021-06-19
phpcms 搜索结果第一页比设定的多一条数据的解决办法2021-01-05
PHPCMS 推荐位输出组图的方法2020-12-30
PHPCMS内容页自定义下拉菜单循环输出数值的方法2020-12-17
PHPCMS v9 search模块搜索栏目名称二次开发2020-02-20
PHPCMS删除文章后数据库中仍有删除文章中的关键词解决办法2019-11-13
PHPCMS中TAG模板、COMMENT模板等不能使用{SITEURL($SITEID)}函数2019-11-13
PHPCMS V9编辑器中新增上传mp4视频(上传视频并插入HTML5的video标签)的功能2019-10-30
PHPCMS一些漏洞修复及解决方案2019-07-10