多個關(guān)鍵詞排序,多關(guān)鍵詞站內(nèi)搜索怎么做
多個關(guān)鍵詞排序,多關(guān)鍵詞站內(nèi)搜索怎么做? 我們做網(wǎng)頁時。某一個頁面有多個關(guān)鍵詞。要搜索相關(guān)的文章時怎么辦?
多個關(guān)鍵詞排序,多關(guān)鍵詞站內(nèi)搜索怎么做? 我們做網(wǎng)頁時。某一個頁面有多個關(guān)鍵詞。要搜索相關(guān)的文章時怎么辦?
多個關(guān)鍵詞排序,多關(guān)鍵詞站內(nèi)搜索怎么做?我們做網(wǎng)頁時。某一個頁面有多個關(guān)鍵詞。要搜索相關(guān)的文章時怎么辦?
PHP搜索主要通過使用SQL語句中的like子句來實(shí)現(xiàn)。如果同時搜索多個關(guān)鍵詞,可以使用union子句來將搜索結(jié)果合并起來。
搜索效果如下
/http://www.artemismd.com/article_26479.html 模板:article/article.html <!--代碼如下--> {if($article['tags'])} <?php $tags = explode(',',$article['tags']); $sql = []; foreach($tags as $v){ $sql[]=" title like '%".$v."%' "; $sql[]=" keywords like '%".$v."%' "; } $tagslist = syDB('article')->findAll(implode(' or ',$sql),null,null,12); ?> {foreach $tagslist as $v} <?php $v['url'] = html_url('article',$v); ?> <li><a title="{$v['title']}" href=" 'url']}">{fun newstr($v['title'],38)} <span>閱讀:{$v['hits']} </span></ a></li> {/foreach} {/if}
//分解用戶輸入的多個關(guān)鍵詞,存入$result數(shù)組 $result = explode('','',$_GET['key']); $mysql_server="localhost"; $mysql_username="數(shù)據(jù)庫用戶名"; $mysql_password="數(shù)據(jù)庫密碼"; $database_conn="數(shù)據(jù)庫名"; //建立數(shù)據(jù)庫鏈接 $conn = mysql_connect($mysql_server,$mysql_username,$mysql_password); //選擇某個數(shù)據(jù)庫 mysql_select_db($database_conn, $conn); //根據(jù)多個關(guān)鍵詞構(gòu)建SQL語句 $query_rs = "SELECT * FROM ("; //根據(jù)每個搜索關(guān)鍵詞構(gòu)建SQL語句 for($i=0;$i<count($result);$i++) { //對第一個關(guān)鍵詞,不使用UNION if($i==0) { $query_rs .= "SELECT * FROM searchtable WHERE title LIKE ''%$result[0]%''OR content LIKE ''%$result[0]%''"; }else{ //對其他關(guān)鍵詞,使用UNION連接 $query_rs .= " UNION SELECT * FROM searchtable WHERE title LIKE''%$result[$i]%'' OR content LIKE ''%$result[$i]%''"; } //對搜索結(jié)果排序 $query_rs .= ") T ORDER BY last_access DESC"; //執(zhí)行SQL語句 $rs = mysql_query($query_rs, $conn) or die(mysql_error()); $row_rs = mysql_fetch_assoc($rs); $totalRows_rs = mysql_num_rows($rs); }
版權(quán)所有:重慶安菲科技有限公司
友情鏈接: