phpcmsv9全站搜索,不限模型

phpcmsv9全站搜索,不限模型,今天又涨知识了。大家都知道,phpcms搜索时按照模型来搜索的,用了这个方法后,就可以全站搜索

简单修改一下v9默认的搜索功能,可以不按模型搜索全站内容
下面是被修改后的search模块中的index.php文件

  1. <?php  
  2. defined('IN_PHPCMS'or exit('No permission resources.');  
  3. pc_base::load_sys_class('form','',0);  
  4. pc_base::load_sys_class('format','',0);  
  5. class index {  
  6.   function __construct() {  
  7.     $this->db = pc_base::load_model('search_model');  
  8.     $this->content_db = pc_base::load_model('content_model');  
  9.   }  
  10.     
  11.   /** 
  12.    * 关键词搜索 
  13.    */  
  14.   public function init() {  
  15.     //获取siteid  
  16.     $siteid = isset($_REQUEST['siteid']) && trim($_REQUEST['siteid']) ? intval($_REQUEST['siteid']) : 1;  
  17.     $SEO = seo($siteid);  
  18.    
  19.     //搜索配置  
  20.     $search_setting = getcache('search');  
  21.     $setting = $search_setting[$siteid];  
  22.    
  23.     $search_model = getcache('search_model_'.$siteid);  
  24.     $type_module = getcache('type_module_'.$siteid);  
  25.    
  26.     if(isset($_GET['q'])) {  
  27.       if(trim($_GET['q'])=='') {  
  28.         header('Location: '.APP_PATH.'index.php?m=search');exit;  
  29.       }  
  30.       $typeid = empty($_GET['typeid']) ? 0 : intval($_GET['typeid']);  
  31.       $time = empty($_GET['time']) || !in_array($_GET['time'],array('all','day','month','year','week')) ? 'all' : trim($_GET['time']);  
  32.       $page = isset($_GET['page']) ? intval($_GET['page']) : 1;  
  33.       $pagesize = 10;  
  34.       $q = safe_replace(trim($_GET['q']));  
  35.       $q = new_html_special_chars(strip_tags($q));  
  36.       $q = str_replace('%'''$q);    //过滤'%',用户全文搜索  
  37.       $search_q = $q;   //搜索原内容  
  38.    
  39.       $sql_time = $sql_tid = '';  
  40.       if($typeid$sql_tid = ' AND typeid = '.$typeid;  
  41.       //按时间搜索  
  42.       if($time == 'day') {  
  43.         $search_time = SYS_TIME - 86400;  
  44.         $sql_time = ' AND adddate > '.$search_time;  
  45.       } elseif($time == 'week') {  
  46.         $search_time = SYS_TIME - 604800;  
  47.         $sql_time = ' AND adddate > '.$search_time;  
  48.       } elseif($time == 'month') {  
  49.         $search_time = SYS_TIME - 2592000;  
  50.         $sql_time = ' AND adddate > '.$search_time;  
  51.       } elseif($time == 'year') {  
  52.         $search_time = SYS_TIME - 31536000;  
  53.         $sql_time = ' AND adddate > '.$search_time;  
  54.       } else {  
  55.         $search_time = 0;  
  56.         $sql_time = '';  
  57.       }  
  58.       if($page==1 && !$setting['sphinxenable']) {  
  59.         //精确搜索  
  60.         $commend = $this->db->get_one("`siteid`= '$siteid' $sql_tid $sql_time AND `data` like '%$q%'");  
  61.       } else {  
  62.         $commend = '';  
  63.       }  
  64.       //如果开启sphinx  
  65.       if($setting['sphinxenable']) {  
  66.         $sphinx = pc_base::load_app_class('search_interface''', 0);  
  67.         $sphinx = new search_interface();  
  68.           
  69.         $offset = $pagesize*($page-1);  
  70.         $res = $sphinx->search($qarray($siteid), array($typeid), array($search_time, SYS_TIME), $offset$pagesize'@weight desc');  
  71.         $totalnums = $res['total'];  
  72.         //如果结果不为空  
  73.         if(!empty($res['matches'])) {  
  74.           $result = $res['matches'];  
  75.         }  
  76.       } else {  
  77.           
  78.         $sql = "`siteid`= '$siteid' $sql_tid $sql_time AND `data` like '%$q%'";  
  79.           
  80.    
  81.         $result = $this->db->listinfo($sql'searchid DESC'$page, 10);  
  82.       }  
  83.        var_dump($result);  
  84.       //如果结果不为空  
  85.       if(!empty($result) || !empty($commend['id'])) {  
  86.         foreach($result as $_v) {  
  87.           if($_v['typeid']) $sids[$_v['typeid']][] = $_v['id'];  
  88.         }  
  89.    
  90.         if(!empty($commend['id'])) {  
  91.           if($commend['typeid']) $sids[$commend['typeid']][] = $commend['id'];  
  92.         }  
  93.         $model_type_cache = getcache('type_model_'.$siteid,'search');  
  94.         $model_type_cache = array_flip($model_type_cache);  
  95.         $data = array();  
  96.         foreach($sids as $_k=>$_val) {  
  97.           $tid = $_k;  
  98.           $ids = array_unique($_val);  
  99.    
  100.           $where = to_sqls($ids'''id');  
  101.           //获取模型id  
  102.           $modelid = $model_type_cache[$tid];  
  103.    
  104.           //是否读取其他模块接口  
  105.           if($modelid) {  
  106.             $this->content_db->set_model($modelid);  
  107.             
  108.             /** 
  109.             * 如果表名为空,则为黄页模型 
  110.             */  
  111.             if(empty($this->content_db->model_tablename)) {  
  112.               $this->content_db = pc_base::load_model('yp_content_model');  
  113.               $this->content_db->set_model($modelid);  
  114.    
  115.             }  
  116.             $datas = $this->content_db->select($where'*');  
  117.           }  
  118.           $data = array_merge($data,$datas);  
  119.         }  
  120.         $pages = $this->db->pages;  
  121.         $totalnums = $this->db->number;  
  122.          
  123.         //如果分词结果为空  
  124.         if(!empty($segment_q)) {  
  125.           $replace = explode(' '$segment_q);  
  126.           foreach($replace as $replace_arr_v) {  
  127.             $replace_arr[] =  '<font color=red>'.$replace_arr_v.'</font>';  
  128.           }  
  129.           foreach($data as $_k=>$_v) {  
  130.             $data[$_k]['title'] = str_replace($replace$replace_arr$_v['title']);  
  131.             $data[$_k]['description'] = str_replace($replace$replace_arr$_v['description']);  
  132.           }  
  133.         } else {  
  134.           foreach($data as $_k=>$_v) {  
  135.             $data[$_k]['title'] = str_replace($q'<font color=red>'.$q.'</font>'$_v['title']);  
  136.             $data[$_k]['description'] = str_replace($q'<font color=red>'.$q.'</font>'$_v['description']);  
  137.           }  
  138.         }  
  139.       }  
  140.       $execute_time = execute_time();  
  141.       $pages = isset($pages) ? $pages : '';  
  142.       $totalnums = isset($totalnums) ? $totalnums : 0;  
  143.       $data = isset($data) ? $data : '';  
  144.         
  145.       include   template('search','list');  
  146.     } else {  
  147.       include   template('search','index');  
  148.     }  
  149.   }  
  150.    
  151.     
  152.   public function public_get_suggest_keyword() {  
  153.     $url = $_GET['url'].'&q='.$_GET['q'];  
  154.     $trust_url = array('c8430fcf851e85818b546addf5bc4dd3');  
  155.     $urm_md5 = md5($url);  
  156.     if (!in_array($urm_md5$trust_url)) exit;  
  157.       
  158.     $res = @file_get_contents($url);  
  159.     if(CHARSET != 'gbk') {  
  160.       $res = iconv('gbk', CHARSET, $res);  
  161.     }  
  162.     echo $res;  
  163.   }  
  164.     
  165.   /** 
  166.    * 提示搜索接口 
  167.    * TODO 暂时未启用,用的是google的接口 
  168.    */  
  169.   public function public_suggest_search() {  
  170.     //关键词转换为拼音  
  171.     pc_base::load_sys_func('iconv');  
  172.     $pinyin = gbk_to_pinyin($q);  
  173.     if(is_array($pinyin)) {  
  174.       $pinyin = implode(''$pinyin);  
  175.     }  
  176.     $this->keyword_db = pc_base::load_model('search_keyword_model');  
  177.     $suggest = $this->keyword_db->select("pinyin like '$pinyin%'"'*', 10, 'searchnums DESC');  
  178.       
  179.     foreach($suggest as $v) {  
  180.       echo $v['keyword']."\n";  
  181.     }  
  182.    
  183.       
  184.   }  
  185. }  
  186. ?>  

然后在header.html模板(不在这里,就在其他的搜索框页面)上面增加一个“不限”的搜索条件,将typeid对应的值为0,search中的index.html和lists.html也做相同处理,效果就出来那,本文由程序员人生网www.fxword.cn整理发布,这样只要不选择模型那么搜索出来的结果就是所有模型中符合条件的数据

猜你喜欢

转载自blog.csdn.net/a1079540945/article/details/80017448