首頁 >CMS教程 >PHPCMS >phpcms不限模型搜尋的設定方法

phpcms不限模型搜尋的設定方法

藏色散人
藏色散人原創
2020-02-05 10:18:152937瀏覽

phpcms不限模型搜尋的設定方法

phpcmsv9全站搜尋,不限模型!

phpcms不限模型搜尋的設定方法

phpcmsv9全站搜尋,不限模型,今天又漲知識了。大家都知道,phpcms搜尋時按照模型來搜尋的,用了這個方法後,就可以全站搜尋

#簡單修改一下v9預設的搜尋功能,可以不按模型搜尋全站內容 

下面是被修改後的search模組中的index.php檔案

<?php
defined(&#39;IN_PHPCMS&#39;) or exit(&#39;No permission resources.&#39;);
pc_base::load_sys_class(&#39;form&#39;,&#39;&#39;,0);
pc_base::load_sys_class(&#39;format&#39;,&#39;&#39;,0);
class index {
  function __construct() {
    $this->db = pc_base::load_model(&#39;search_model&#39;);
    $this->content_db = pc_base::load_model(&#39;content_model&#39;);
  }
  
  /**
   * 关键词搜索
   */
  public function init() {
    //获取siteid
    $siteid = isset($_REQUEST[&#39;siteid&#39;]) && trim($_REQUEST[&#39;siteid&#39;]) ? intval($_REQUEST[&#39;siteid&#39;]) : 1;
    $SEO = seo($siteid);
 
    //搜索配置
    $search_setting = getcache(&#39;search&#39;);
    $setting = $search_setting[$siteid];
 
    $search_model = getcache(&#39;search_model_&#39;.$siteid);
    $type_module = getcache(&#39;type_module_&#39;.$siteid);
 
    if(isset($_GET[&#39;q&#39;])) {
      if(trim($_GET[&#39;q&#39;])==&#39;&#39;) {
        header(&#39;Location: &#39;.APP_PATH.&#39;index.php?m=search&#39;);exit;
      }
      $typeid = empty($_GET[&#39;typeid&#39;]) ? 0 : intval($_GET[&#39;typeid&#39;]);
      $time = empty($_GET[&#39;time&#39;]) || !in_array($_GET[&#39;time&#39;],array(&#39;all&#39;,&#39;day&#39;,&#39;month&#39;,&#39;year&#39;,&#39;week&#39;)) ? &#39;all&#39; : trim($_GET[&#39;time&#39;]);
      $page = isset($_GET[&#39;page&#39;]) ? intval($_GET[&#39;page&#39;]) : 1;
      $pagesize = 10;
      $q = safe_replace(trim($_GET[&#39;q&#39;]));
      $q = new_html_special_chars(strip_tags($q));
      $q = str_replace(&#39;%&#39;, &#39;&#39;, $q);//过滤&#39;%&#39;,用户全文搜索
      $search_q = $q;//搜索原内容
 
      $sql_time = $sql_tid = &#39;&#39;;
      if($typeid) $sql_tid = &#39; AND typeid = &#39;.$typeid;
      //按时间搜索
      if($time == &#39;day&#39;) {
        $search_time = SYS_TIME - 86400;
        $sql_time = &#39; AND adddate > &#39;.$search_time;
      } elseif($time == &#39;week&#39;) {
        $search_time = SYS_TIME - 604800;
        $sql_time = &#39; AND adddate > &#39;.$search_time;
      } elseif($time == &#39;month&#39;) {
        $search_time = SYS_TIME - 2592000;
        $sql_time = &#39; AND adddate > &#39;.$search_time;
      } elseif($time == &#39;year&#39;) {
        $search_time = SYS_TIME - 31536000;
        $sql_time = &#39; AND adddate > &#39;.$search_time;
      } else {
        $search_time = 0;
        $sql_time = &#39;&#39;;
      }
      if($page==1 && !$setting[&#39;sphinxenable&#39;]) {
        //精确搜索
        $commend = $this->db->get_one("`siteid`= &#39;$siteid&#39; $sql_tid $sql_time AND `data` like &#39;%$q%&#39;");
      } else {
        $commend = &#39;&#39;;
      }
      //如果开启sphinx
      if($setting[&#39;sphinxenable&#39;]) {
        $sphinx = pc_base::load_app_class(&#39;search_interface&#39;, &#39;&#39;, 0);
        $sphinx = new search_interface();
        
        $offset = $pagesize*($page-1);
        $res = $sphinx->search($q, array($siteid), array($typeid), array($search_time, SYS_TIME), $offset, $pagesize, &#39;@weight desc&#39;);
        $totalnums = $res[&#39;total&#39;];
        //如果结果不为空
        if(!empty($res[&#39;matches&#39;])) {
          $result = $res[&#39;matches&#39;];
        }
      } else {
        
        $sql = "`siteid`= &#39;$siteid&#39; $sql_tid $sql_time AND `data` like &#39;%$q%&#39;";
        
 
        $result = $this->db->listinfo($sql, &#39;searchid DESC&#39;, $page, 10);
      }
       var_dump($result);
      //如果结果不为空
      if(!empty($result) || !empty($commend[&#39;id&#39;])) {
        foreach($result as $_v) {
          if($_v[&#39;typeid&#39;]) $sids[$_v[&#39;typeid&#39;]][] = $_v[&#39;id&#39;];
        }
 
        if(!empty($commend[&#39;id&#39;])) {
          if($commend[&#39;typeid&#39;]) $sids[$commend[&#39;typeid&#39;]][] = $commend[&#39;id&#39;];
        }
        $model_type_cache = getcache(&#39;type_model_&#39;.$siteid,&#39;search&#39;);
        $model_type_cache = array_flip($model_type_cache);
        $data = array();
        foreach($sids as $_k=>$_val) {
          $tid = $_k;
          $ids = array_unique($_val);
 
          $where = to_sqls($ids, &#39;&#39;, &#39;id&#39;);
          //获取模型id
          $modelid = $model_type_cache[$tid];
 
          //是否读取其他模块接口
          if($modelid) {
            $this->content_db->set_model($modelid);
          
            /**
            * 如果表名为空,则为黄页模型
            */
            if(empty($this->content_db->model_tablename)) {
              $this->content_db = pc_base::load_model(&#39;yp_content_model&#39;);
              $this->content_db->set_model($modelid);
 
            }
            $datas = $this->content_db->select($where, &#39;*&#39;);
          }
          $data = array_merge($data,$datas);
        }
        $pages = $this->db->pages;
        $totalnums = $this->db->number;
       
        //如果分词结果为空
        if(!empty($segment_q)) {
          $replace = explode(&#39; &#39;, $segment_q);
          foreach($replace as $replace_arr_v) {
            $replace_arr[] =  &#39;<font color=red>&#39;.$replace_arr_v.&#39;</font>&#39;;
          }
          foreach($data as $_k=>$_v) {
            $data[$_k][&#39;title&#39;] = str_replace($replace, $replace_arr, $_v[&#39;title&#39;]);
            $data[$_k][&#39;description&#39;] = str_replace($replace, $replace_arr, $_v[&#39;description&#39;]);
          }
        } else {
          foreach($data as $_k=>$_v) {
            $data[$_k][&#39;title&#39;] = str_replace($q, &#39;<font color=red>&#39;.$q.&#39;</font>&#39;, $_v[&#39;title&#39;]);
            $data[$_k][&#39;description&#39;] = str_replace($q, &#39;<font color=red>&#39;.$q.&#39;</font>&#39;, $_v[&#39;description&#39;]);
          }
        }
      }
      $execute_time = execute_time();
      $pages = isset($pages) ? $pages : &#39;&#39;;
      $totalnums = isset($totalnums) ? $totalnums : 0;
      $data = isset($data) ? $data : &#39;&#39;;
      
      includetemplate(&#39;search&#39;,&#39;list&#39;);
    } else {
      includetemplate(&#39;search&#39;,&#39;index&#39;);
    }
  }
 
  
  public function public_get_suggest_keyword() {
    $url = $_GET[&#39;url&#39;].&#39;&q=&#39;.$_GET[&#39;q&#39;];
    $trust_url = array(&#39;c8430fcf851e85818b546addf5bc4dd3&#39;);
    $urm_md5 = md5($url);
    if (!in_array($urm_md5, $trust_url)) exit;
    
    $res = @file_get_contents($url);
    if(CHARSET != &#39;gbk&#39;) {
      $res = iconv(&#39;gbk&#39;, CHARSET, $res);
    }
    echo $res;
  }
  
  /**
   * 提示搜索接口
   * TODO 暂时未启用,用的是google的接口
   */
  public function public_suggest_search() {
    //关键词转换为拼音
    pc_base::load_sys_func(&#39;iconv&#39;);
    $pinyin = gbk_to_pinyin($q);
    if(is_array($pinyin)) {
      $pinyin = implode(&#39;&#39;, $pinyin);
    }
    $this->keyword_db = pc_base::load_model(&#39;search_keyword_model&#39;);
    $suggest = $this->keyword_db->select("pinyin like &#39;$pinyin%&#39;", &#39;*&#39;, 10, &#39;searchnums DESC&#39;);
    
    foreach($suggest as $v) {
      echo $v[&#39;keyword&#39;]."\n";
    }
 
    
  }
}
?>

然後在header.html模板(不在這裡,就在其他的搜尋框頁面)上面增加一個「不限」的搜尋條件,將typeid對應的值為0,search中的index.html和lists.html也做相同處理,效果就出來那,這樣只要不選擇模型那麼搜尋出來的結果就是所有模型中符合條件的資料。

PHP中文網,大量的免費PHPCMS教學,歡迎線上學習!

以上是phpcms不限模型搜尋的設定方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn