gpt4 book ai didi

php - 多词搜索

转载 作者:行者123 更新时间:2023-11-30 23:11:01 25 4
gpt4 key购买 nike

我做了一个简单的搜索引擎,可以搜索我的数据库。它是这样工作的:您在复选框中选中要搜索的词。结果使用表格发送。问题是它只搜索检查的最后一个词。这意味着我检查 3 个单词时它只显示检查的最后一个单词的结果。这是我的表格:

<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>"> 
<p>Search for:
</p>
Books: <input type="checkbox" name='search' value="Books">
Movies: <input type="checkbox" name='search' value="Movies">
Outdoor: <input type="checkbox" name='search' value="Outdoor">
Indoor: <input type="checkbox" name='search' value="Indoor">
</p>
<p><input type='submit' value='Search'></p>
</form>

这是我生成结果的代码:

    <?php 
if(isset($_POST['search']))
{
$connx = mysql_connect('localhost', 'USER', 'PASSWORD') or die("connx");
$db = mysql_select_db('DB_NAME') or die(mysql_error());

# convert to upper case, trim it, and replace spaces with "|":
$search = (ini_get('magic_quotes_gpc')) ? stripslashes($_POST['search']) :
$_POST['search'];
$search = mysql_real_escape_string($search);
$search = strtoupper(preg_replace('/\s+/', '|', trim($_POST['search'])));

# create a MySQL REGEXP for the search:
$regexp = "REGEXP '[[:<:]]($search)[[:>:]]'";
$query = "SELECT * FROM `galleries` WHERE UPPER(`keywords1`) $regexp OR ".
"`keywords2` $regexp";
$result = mysql_query($query) or die($query . " - " . mysql_error());

echo "<table>\n";
while($row = mysql_fetch_assoc($result))

{
echo "<tr>";
echo "<td><img src=../thumbs/{$row['type']}/{$row['folder']}/{$row['date']}-{$row['num']}/{$row['thumbimage']} border=1></td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['date']}</td>";
echo "<td><a href=../view.php?id={$row['id']} target=blank>View</a></td>";
echo "</tr>\n";
}
}
?>

谁能帮我告诉我如何让搜索引擎搜索和/或显示超过 1 个词的搜索结果?

最佳答案

您有同名的输入字段,因此您将获得为搜索字段创建数组所需的最后一个字段,例如 name="search[]" 并循环查询数组值(value)观

Books: <input type="checkbox" name='search[]' value="Books"> 
Movies: <input type="checkbox" name='search[]' value="Movies">
Outdoor: <input type="checkbox" name='search[]' value="Outdoor">
Indoor: <input type="checkbox" name='search[]' value="Indoor">

像这样

$regexp ="";
$searharray =$_POST['search'];
$count=count($searharray);
$index=1;
foreach($searharray as $s){

$s = (ini_get('magic_quotes_gpc')) ? stripslashes($s) :$s;
$s= mysql_real_escape_string($s);
$s = strtoupper(preg_replace('/\s+/', '|', trim($s)));

$regexp .= " UPPER(`keywords1`) REGEXP '[[:<:]](".$s.")[[:>:]]' OR `keywords2` REGEXP '[[:<:]](".$s.")[[:>:]]'";
if($index<$count){
$regexp .=" OR ";
}
$index++
}

$query = "SELECT * FROM `galleries` WHERE $regexp";

关于php - 多词搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19715021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com