gpt4 book ai didi

php - 如何在 php 中为搜索创建过滤器

转载 作者:太空宇宙 更新时间:2023-11-03 11:33:59 25 4
gpt4 key购买 nike

我对 php 有这个问题。我想要做的是拥有一个具有三个过滤器的搜索引擎:按标题、描述和价格搜索。这是我的索引 html。

<form style="text-align:center;" method="get" action="search.php">
<label>
Search
<input type="text"name="keywords" autocomplete="off">
</label>
<input type="submit" value="Search"><br>
</form>

通过使用操作方法,我正在调用 search.php 文件。

if(isset($_GET['keywords'])) {
$keywords = $db->escape_string($_GET['keywords']);

$query = $db->query("SELECT title, description, price FROM products WHERE title LIKE '%{$keywords}%' OR description LIKE '%{$keywords}%' OR price LIKE '%{$keywords}%'");
}

它不会将所有结果合并为一个,因为我使用的是相同的变量关键字。现在的问题是我没有过滤器,因为我不确定如何创建它。也许我想使用这三个选项进行下拉选择:标题、描述、价格。但我不知道如何让我的 php 代码查看选择了哪一个。或者也许我的想法有更好的解决方案?我应该使用不同的变量,例如关键字 was 还是什么?

最佳答案

这很简单.. 添加一个选择标签。这是一个例子。

<!doctype html>
<html>
<head>
<title>filter</title>
</head>
<body>
<form method="post" action="test1.php">
<select name="keywords" >
<option value="title">title</option>
<option value="description">description</option>
<option value="price">price</option>
</select>
<input type="submit">
</form>

<?php print_r($_POST);
if(isset($_GET['keywords'])) {
$keywords = $db->escape_string($_GET['keywords']);

$query = $db->query("SELECT title, description, price FROM products WHERE '%{$keywords}%' LIKE '%{$keywords}%' ");
}

?>
<br>


</body>
</html>

关于php - 如何在 php 中为搜索创建过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47486870/

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