我想创建一个搜索框,而不是在查询中使用where cluse来匹配字段中的确切短语,而是希望搜索框能够执行语义搜索,以便当用户输入短语的一部分时,它会显示能够匹配短语含义的适当结果。当你尝试通过谷歌进行搜索时,谷歌到底是如何工作的。这有可能实现吗?有人有什么想法吗?
下面是我目前使用的代码,以便能够使用数据库查看行是否与在搜索框中输入的短语完全匹配:
<?php
//connected to DB
foreach (array('questioncontent') as $varname) {
$questioncontent = (isset($_POST[$varname])) ? $_POST[$varname] : '';
}
?>
<p>Search for a previous question by entering in a phrase in the search box below and submitting the phrase</p>
<form action="previousquestions.php" method="post" id="modalform">
<p>Search: <input type="text" name="questioncontent" value="<?php echo $questioncontent; ?>" /></p>
<p><input id="searchquestion" name="searchQuestion" type="submit" value="Search" /></p>
</form>
<?php
if (isset($_POST['searchQuestion'])) {
$questionquery = "SELECT QuestionContent FROM Question
WHERE(QuestionContent = '".mysql_real_escape_string($questioncontent)."')";
$questionnum = mysql_num_rows($questionresult = mysql_query($questionquery));
if($questionnum !=0){
$output = "";
while ($questionrow = mysql_fetch_array($questionresult)) {
$output .= "
<table>
<tr>
<td>{$questionrow['QuestionContent']}</td>
</tr>";
}
$output .= " </table>";
echo $output;
}
}发布于 2012-05-25 01:51:07
如果你正在寻找一个PHP搜索引擎教程,看看这个YouTube video -它可能会有帮助。
发布于 2012-05-25 01:42:31
您正在寻找一个“自动完成”函数。尝试使用php +mysql的这些教程:
jquery-php-mysql-ajax-autocomplete
Stackoverflow jquery autocomplete
Blog on Autocomplete
ajax-autocomplete-using-php-mysql
jquery-ui-autocomplete-widget-with-php-and-mysql
https://stackoverflow.com/questions/10742561
复制相似问题