简单修改一下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($q, array($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 //如果结果不为空 84 if(!empty($result) || !empty($commend['id'])) { 85 foreach($result as $_v) { 86 if($_v['typeid']) $sids[$_v['typeid']][] = $_v['id']; 87 } 88 89 if(!empty($commend['id'])) { 90 if($commend['typeid']) $sids[$commend['typeid']][] = $commend['id']; 91 } 92 $model_type_cache = getcache('type_model_'.$siteid,'search'); 93 $model_type_cache = array_flip($model_type_cache); 94 $data = array(); 95 foreach($sids as $_k=>$_val) { 96 $tid = $_k; 97 $ids = array_unique($_val); 98 99 $where = to_sqls($ids, '', 'id'); 100 //获取模型id 101 $modelid = $model_type_cache[$tid]; 102 103 //是否读取其他模块接口 104 if($modelid) { 105 $this->content_db->set_model($modelid); 106 107 /** 108 * 如果表名为空,则为黄页模型 109 */ 110 if(empty($this->content_db->model_tablename)) { 111 $this->content_db = pc_base::load_model('yp_content_model'); 112 $this->content_db->set_model($modelid); 113 114 } 115 $datas = $this->content_db->select($where, '*'); 116 } 117 $data = array_merge($data,$datas); 118 } 119 $pages = $this->db->pages; 120 $totalnums = $this->db->number; 121 122 //如果分词结果为空 123 if(!empty($segment_q)) { 124 $replace = explode(' ', $segment_q); 125 foreach($replace as $replace_arr_v) { 126 $replace_arr[] = '<font color=red>'.$replace_arr_v.'</font>'; 127 } 128 foreach($data as $_k=>$_v) { 129 $data[$_k]['title'] = str_replace($replace, $replace_arr, $_v['title']); 130 $data[$_k]['description'] = str_replace($replace, $replace_arr, $_v['description']); 131 } 132 } else { 133 foreach($data as $_k=>$_v) { 134 $data[$_k]['title'] = str_replace($q, '<font color=red>'.$q.'</font>', $_v['title']); 135 $data[$_k]['description'] = str_replace($q, '<font color=red>'.$q.'</font>', $_v['description']); 136 } 137 } 138 } 139 $execute_time = execute_time(); 140 $pages = isset($pages) ? $pages : ''; 141 $totalnums = isset($totalnums) ? $totalnums : 0; 142 $data = isset($data) ? $data : ''; 143 144 include template('search','list'); 145 } else { 146 include template('search','index'); 147 } 148 } 149 150 151 public function public_get_suggest_keyword() { 152 $url = $_GET['url'].'&q='.$_GET['q']; 153 $trust_url = array('c8430fcf851e85818b546addf5bc4dd3'); 154 $urm_md5 = md5($url); 155 if (!in_array($urm_md5, $trust_url)) exit; 156 157 $res = @file_get_contents($url); 158 if(CHARSET != 'gbk') { 159 $res = iconv('gbk', CHARSET, $res); 160 } 161 echo $res; 162 } 163 164 /** 165 * 提示搜索接口 166 * TODO 暂时未启用,用的是google的接口 167 */ 168 public function public_suggest_search() { 169 //关键词转换为拼音 170 pc_base::load_sys_func('iconv'); 171 $pinyin = gbk_to_pinyin($q); 172 if(is_array($pinyin)) { 173 $pinyin = implode('', $pinyin); 174 } 175 $this->keyword_db = pc_base::load_model('search_keyword_model'); 176 $suggest = $this->keyword_db->select("pinyin like '$pinyin%'", '*', 10, 'searchnums DESC'); 177 178 foreach($suggest as $v) { 179 echo $v['keyword']." "; 180 } 181 182 183 } 184 } 185 ?>
静态页面修改phpcms emplatesdefaultsearch:
index.html修改:
<div class="l" id="search"> <a href="javascript:;" onclick="setmodel(0, $(this));" {if $typeid==0} class="on" {/if}>不限</a> - {php $j=0} {loop $search_model $k=>$v} {php $j++;} <a href="javascript:;" onclick="setmodel({$v['typeid']}, $(this));">{$v['name']}</a>{if $j != count($search_model)} - {/if} {/loop} {php unset($j);} </div>
<input type="hidden" name="typeid" value="0" id="typeid"/>
list.html修改:
<div class="l" id="search"> <a href="javascript:;" onclick="setmodel(0, $(this),{$siteid}, '{$q}');" {if $typeid==0} class="on" {/if}>不限</a> - {php $j=0} {loop $search_model $k=>$v} {php $j++;} <a href="javascript:;" onclick="setmodel({$v['typeid']}, $(this),{$siteid}, '{$q}');" {if $v['typeid']==$typeid} class="on"{/if}>{$v['name']}</a>{if $j != count($search_model)} - {/if} {/loop} </div>
<div> <span>网页结果</span> <ul> <li><a href="?m=search&c=index&a=init&typeid=0&q={urlencode($search_q)}&siteid={$siteid}&time={$time}" {if $typeid==0} class="ac"{/if}>不限</a> </li> {loop $search_model $k=>$v} <li><a href="?m=search&c=index&a=init&typeid={$v['typeid']}&q={urlencode($search_q)}&siteid={$siteid}&time={$time}" {if $v['typeid']==$typeid} class="ac"{/if}>{$v['name']}</a> </li> {/loop} </ul> </div>