[英]Search engine with multiple keywords
我有一個正在運行的搜索引擎,但只有當我搜索一個單詞時。 每當我搜索多個關鍵字時,我只得到1個結果。
示例:在我的數據庫中,我有'test'和'hello'等標簽; 每當我輸入“test hello”並單擊“Search”時,它會顯示:
1結果hello(這是帶有tag = hello的帖子的標題)。
我的代碼(search.php - 我獲得搜索結果的頁面):
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
if(!$button) {
echo "you didn't submit a keyword";
} else {
if(strlen($search)<=1) {
echo "Search term too short";
} else {
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","root","root");
mysql_select_db("myschool");
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each) {
$x = NULL;
$construct = NULL;
$x++;
if($x==1) {
$construct .="tag LIKE '%$search_each%'";
} else {
$construct .="OR tag LIKE '%$search_each%'";
}
$construct ="SELECT * FROM posts WHERE $construct";
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);
if ($foundnum==0) {
echo "Sorry, there are no matching result for <b>$search</b>.";
} else {
echo "$foundnum results found !<p>";
while($runrows = mysql_fetch_assoc($run)) {
$title = $runrows ['title'];
$tag = $runrows ['tag'];
echo "<a href='#'><b>$title</b></a><br>";
}
}
}
}
}
?>
問題可能在$x=++
部分,因為我認為引擎沒有顯示甚至搜索數據庫中的所有行,並且在num行計數> 1時不顯示。
先謝謝你們。
編輯:
你搜索了hello test postare
找到1個結果! HELLO 1搜索結果!
測試1結果發現!
如何將結果添加到一個位置,而不是每次為不同的關鍵字找到新結果時都說出來?
您的代碼有幾個問題。 你錯過了"
在這條線上:
echo "Sorry, there are no matching result for <b>$search</b>";
而最后的else
沒有if
您需要在foreach語句之前啟動$ x變量,如果要將其用作整數,則不要將其設置為null。
$ construct變量有相同的錯誤,你必須有三次相同的響應,那是因為你必須在調用mysql select之前關閉foreach語句。
$x = 1;
$construct = NULL;
foreach($search_exploded as $search_each)
{
if($x==1) {
$construct .="tag LIKE '%$search_each%'";
} else {
$construct .="OR tag LIKE '%$search_each%'";
}
$x++;
}
$select ="SELECT * FROM posts WHERE $construct";
...
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
if(!$button) {
echo "you didn't submit a keyword";
} else {
if(strlen($search)<=1) {
echo "Search term too short";
} else {
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","root","root");
mysql_select_db("myschool");
$search_exploded = explode (" ", $search);
$x = 1;
$construct = '';
foreach($search_exploded as $search_each) {
if($x==1) {
$construct .="tag LIKE '%$search_each%'";
} else {
$construct .="OR tag LIKE '%$search_each%'";
}
$x++;
}
$select ="SELECT * FROM posts WHERE $construct";
$run = mysql_query($select);
$foundnum = mysql_num_rows($run);
if ($foundnum==0) {
echo "Sorry, there are no matching result for <b>$search</b>.";
} else {
echo "$foundnum results found !<p>";
while($runrows = mysql_fetch_assoc($run)) {
$title = $runrows ['title'];
$tag = $runrows ['tag'];
echo "<a href='#'><b>$title</b></a><br>";
}
}
}
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.