Home  >  Article  >  CMS Tutorial  >  How to set up phpcms unlimited model search

How to set up phpcms unlimited model search

藏色散人
藏色散人Original
2020-02-05 10:18:152846browse

How to set up phpcms unlimited model search

phpcmsv9 full site search, no model restrictions!

How to set up phpcms unlimited model search

phpcmsv9 full site search, unlimited model, today I gained more knowledge. As we all know, phpcms searches based on models. After using this method, you can search the entire site

Simply modify the default search function of v9, and you can search the entire site content without modeling

The following is the index.php file in the modified search module

<?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";
    }
 
    
  }
}
?>

Then add an "unlimited" to the header.html template (not here, just on other search box pages) For the search conditions, set the corresponding value of typeid to 0, and do the same processing for index.html and lists.html in the search, and the effect will be there. In this way, as long as the model is not selected, the search results will be the data that meets the conditions in all models.

PHP Chinese website, a large number of free PHPCMS tutorials, welcome to learn online!

The above is the detailed content of How to set up phpcms unlimited model search. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn