我目前正在使用
$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE ";
foreach ($terms as $each) {
$i++;
if ($i == 1)
$query .= "keywords = '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
// connect
mysql_connect("localhost", "root", "password");
mysql_select_db("search");
$query = mysql_query($query);
$numrows = mysql_num_rows ($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$links = $row['link'];
echo "<h2>$title</h2><p>$description</p>";
}
}
else
echo "No Search Results have been found for \"<b>$k</b>\"";
// disconnect
mysql_close();
?>
我从教程中学到的这段代码开始我的搜索引擎。说实话,我不懂任何PHP,理解这里发生的事情是一场真正的斗争。
如何操作这段代码,以便我搜索&#34; Swarthmore College&#34;或者&#34; Swarthmore&#34;,斯沃斯莫尔的结果将出现。我需要确保只输入&#34; College&#34;或者&#34; swar&#34;或&#34; a&#34;不会触发Swarthmore的结果。 但我需要像#34;大学&#34;,&#34;&#34;和&#34;&#34;芝加哥&#34;一起触发大学搜索,如芝加哥大学。&#34;它也应该由&#34; UChicago等缩写引发。&#34;
对不起,如果要问这个问题很多,但我完全迷失了。 提前谢谢。
答案 0 :(得分:0)
您应该包含elseif
clausule。代码是:
if ($numrows == 1) {
// You dont need to iterate if you want to show only unique result
$row = mysql_fetch_assoc($query);
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$links = $row['link'];
echo "<h2>$title</h2><p>$description</p>";
}
elseif ($numrows > 1)
echo "More than one Search Results have been found for \"<b>$k</b>\". Please be more specific ";
else
echo "No Search Results have been found for \"<b>$k</b>\"";