具有多个关键字的搜索引擎


Search engine with multiple keywords

我有一个正在运行的搜索引擎,但仅当我搜索一个单词时。每当我搜索多个关键词时,我只得到一个结果。

示例:在我的数据库中,我有"test"answers"hello"这样的标签;每当我输入"测试你好"并点击"搜索"时,它就会显示:

1结果hello(这是带有标签=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时也没有显示。

提前感谢各位。

编辑:

我现在用上面的代码得到了多个结果,但我是以这种形式得到的:

你搜索了你好测试postare

找到1个结果!你好找到1个结果!

测试找到1个结果!

postare noua

我怎么能让它把结果加在一个地方,而不是每次发现不同关键字的新结果时都说出来?

您需要在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>";
            }
        }
    }
}
?>

您的代码有几个问题。你错过了这条线上的一个"

echo "Sorry, there are no matching result for <b>$search</b>";

并且最后一个else没有if