具有多个关键字的搜索引擎
Search engine with multiple keywords
我的搜索引擎正在工作,但仅当我搜索 1 个词时。每当我搜索多个关键字时,我只会得到 1 个结果。
示例:在我的数据库中,我有像 'test' 和 'hello' 这样的标签;
每当我输入 "test hello" 并单击 "Search" 时,它都会显示:
1 结果你好(这是带有标签 = hello 的 post 的标题)。
我的代码(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 post是
找到 1 个结果!
你好
找到 1 个结果!
测试
找到 1 个结果!
post是新手
我怎样才能让它在 1 个地方添加结果,而不是每次它为不同的关键字找到新结果时都说出来?
您的代码有几个问题。你错过了这一行的 "
:
echo "Sorry, there are no matching result for <b>$search</b>";
最后一个else
没有if
在foreach语句之前需要启动$x变量,如果要作为整数使用则不要设置为null。
$construct 变量有相同的错误,你必须有相同的响应 3 次,那是因为你必须在调用 mysql select.[=12 之前关闭 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>";
}
}
}
}
?>
我的搜索引擎正在工作,但仅当我搜索 1 个词时。每当我搜索多个关键字时,我只会得到 1 个结果。
示例:在我的数据库中,我有像 'test' 和 'hello' 这样的标签; 每当我输入 "test hello" 并单击 "Search" 时,它都会显示:
1 结果你好(这是带有标签 = hello 的 post 的标题)。
我的代码(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 post是
找到 1 个结果! 你好 找到 1 个结果!
测试 找到 1 个结果!
post是新手
我怎样才能让它在 1 个地方添加结果,而不是每次它为不同的关键字找到新结果时都说出来?
您的代码有几个问题。你错过了这一行的 "
:
echo "Sorry, there are no matching result for <b>$search</b>";
最后一个else
没有if
在foreach语句之前需要启动$x变量,如果要作为整数使用则不要设置为null。
$construct 变量有相同的错误,你必须有相同的响应 3 次,那是因为你必须在调用 mysql select.[=12 之前关闭 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>";
}
}
}
}
?>