在我的搜索表单中,如果用户输入“好”,它会显示所有包含关键字“好”的结果。但是,如果用户输入“good sweetest”,则不会显示任何结果,因为没有两个单词一起出现的记录;但是出现在不同地方的条目中。
例如,记录说:
一个好的动作是一个永远的存储和一个纯粹的收益
用户输入'good',它会显示这条记录,但是如果用户输入'good' + 'pure',它不会显示任何东西。或者如果记录包含关键字“good-deeds”,并且如果用户键入“good deeds”而没有连字符,则它不会显示任何内容。
我想要的是,如果用户输入“good”+“pure”或“good deeds”,它应该记录包含突出显示它们的这些关键字。
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>
搜索.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>