【问题标题】:Search engine with multiple keywords具有多个关键字的搜索引擎
【发布时间】:2015-02-28 23:10:42
【问题描述】:

我有一个 正在 工作的搜索引擎,但只有当我搜索 1 个单词时。每当我搜索多个关键字时,我只会得到 1 个结果。

示例:在我的数据库中,我有像 'test' 和 'hello' 这样的标签; 每当我输入“test hello”并单击“搜索”时,它都会显示:

1 个结果你好(这是带有标签 = 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=++ 部分,因为我认为引擎没有显示甚至没有搜索数据库中的所有行,并且当行数> 1 时也没有显示。

提前谢谢各位。

编辑:

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

您搜索的是 hello test postare

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

测试 找到 1 个结果!

postare noua

我怎样才能让它在一个地方添加结果,而不是每次找到不同关键字的新结果时都说出来?

【问题讨论】:

  • 使用mysqli 而不是mysql。您的数据库连接应该在单独的安全页面上。正确的 URL 中不会有空格。查看rawurldecode()。为什么查询会包含WHERE OR?。
  • 谢谢,但它仍然没有回答我的问题,为什么我没有得到多个关键字的多个结果?
  • 查看全文搜索。

标签: php search field keyword


【解决方案1】:

你需要在foreach语句之前启动$x变量,如果你想作为整数使用,不要设置为null。

$construct 变量也有同样的错误,你必须有 3 次相同的响应,那是因为你必须在调用 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>";

            }
        }
    }
}
?>

【讨论】:

  • 好吧,老实说,它对我有用,现在我的问题是,每次它找到一个关键字的结果时,它都会显示它找到了多少结果。我把它放在上面。
  • 这搞砸了我的代码,页面上没有显示任何内容,而且我相信您忘记添加 $x=++;无论如何,我把我的新问题放在上面的编辑中;谢谢。
【解决方案2】:

您的代码有几个问题。您在此行错过了"

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

最后一个else 没有if

【讨论】:

  • 最后一个else有上面的if,反正我都修好了,用多个关键字还是没有得到多个结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-21
  • 1970-01-01
  • 2011-07-24
  • 2011-10-15
  • 2015-01-16
  • 2012-10-30
相关资源
最近更新 更多