Rumah >Tutorial CMS >PHPCMS >phpcms不限模型搜索的设置方法

phpcms不限模型搜索的设置方法

藏色散人
藏色散人asal
2020-02-05 10:18:152949semak imbas

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教程,欢迎在线学习!

Atas ialah kandungan terperinci phpcms不限模型搜索的设置方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn