1. 程式人生 > >phpcms v9按照新增的欄目搜尋

phpcms v9按照新增的欄目搜尋

1、修改後臺新增、修改內容時新增欄目id到搜尋表中
  找到  phpcms\model\content_model.class.php 大概113、302行左右,一共兩處
  $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 = '$_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 做個索引,這樣就完成了,前臺呼叫的程式碼為:

 <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="12" id="catid"/>
                <input type="hidden" name="siteid" value="{$siteid}" id="siteid"/>
<input type="text" autocomplete="off" id="input" name="q" value="請輸入關鍵詞,如 北京大學" />
<input type="submit" onMouseOut="this.className=''" onMouseDown="this.className='mousedown'" onMouseOver="this.className='hover'" value="搜尋一下" id="search-button">