,如果用戶輸入'good',它會顯示所有包含關鍵字'good'的結果。然而,如果用戶輸入「最好的最甜蜜」,則它不顯示結果,因爲沒有兩個詞彙一起出現的記錄;但出現在不同地方的入口處。在我的搜索表單中顯示多個單詞的搜索結果
例如,記錄說:
一個很好的動作是一個不斷剩餘 店和純產在「好」,它會顯示這個
用戶類型記錄,但如果用戶輸入「好」+「純」,則不會顯示任何內容。或者如果記錄中包含關鍵字「善行」,並且用戶在沒有連字符的情況下輸入「好行爲」,則不會顯示任何內容。
我想要的是,如果用戶輸入'好'+'純'或'好行爲',則應該包含這些關鍵字的記錄突出顯示它們。
的search.php代碼:
$search_result = "";
$search_result = $_POST["q"];
$search_result = trim($search_result);
//Check if the string is empty
if ($search_result == "") {
echo "<p class='error'>Search Error. Please Enter Your Search Query.</p>" ;
exit();
}
if ($search_result == "%" || $search_result == "_" || $search_result == "+") {
echo "<p class='error1'>Search Error. Please Enter a Valid Search Query.</p>" ;
exit();
}
$result = mysql_query('SELECT cQuotes, vAuthor, cArabic, vReference FROM thquotes WHERE cQuotes LIKE "%' . mysql_real_escape_string($search_result) .'%" ORDER BY idQuotes DESC', $conn)
or die ('Error: '.mysql_error());
function h($s) {
echo htmlspecialchars($s, ENT_QUOTES);
}
function highlightWords($string, $word)
{
$string = preg_replace("/".preg_quote($word, "/")."/i", "<span class='highlight'>$0</span>", $string);
/*** return the highlighted string ***/
return $string;
}
?>
<div class="caption">Search Results</div>
<div class="center_div">
<table>
<?php while ($row= mysql_fetch_array($result, MYSQL_ASSOC)) {
$cQuote = highlightWords(htmlspecialchars($row['cQuotes']), $search_result);
?>
<tr>
<td style="text-align:right; font-size:18px;"><?php h($row['cArabic']); ?></td>
<td style="font-size:16px;"><?php echo $cQuote; ?></td>
<td style="font-size:12px;"><?php h($row['vAuthor']); ?></td>
<td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']); ?></td>
</tr>
<?php } ?>
</table>
</div>
search.html:
<form name="myform" class="wrapper">
<input type="text" name="q" onkeyup="showUser()" class="txt_search"/>
<input type="button" name="button" onclick="showUser()" class="button"/>
<p>
<div id="txtHint"></div>
</form>
我的表類型是INNODB。 。不幸的是它說它不支持FULLTEXT。 – input 2010-05-01 20:58:51
在這種情況下,我建議你去看看http://stackoverflow.com/questions/1381186/fulltext-search-with-innodb,它完全處理如何解決這個問題。如果一個專門的搜索引擎(就像那篇文章中提到的那樣)對你來說是不可能的,你可以做其他事情,比如使用觸發器將你的InnoDB數據複製到MyISAM表並在該表上搜索,或者手動滾動你自己的解決方案分裂的內容和鏈接到正確的職位,如加布裏埃爾建議。 – 2010-05-01 21:14:11
謝謝你的回答。決定繼續使用php替代品。 http://www.acuras.co.uk/articles/2-php-multi-word-mysql-search-algorithm-and-output – input 2010-05-03 10:48:22