我正在尝试创建一个多选项搜索。我到处寻找,似乎找不到合适的解决方案,尽管很多网站都有这个。 所以我有我的html表单(有点截断,不会让你烦恼):
<form method="get" id="searchForm" name="searchForm" action="search-text.php" >
<select name="make" id="make">
<option value="">ANY MAKE</option>
<option value="make1">MAKE1</option>
<option value="make2">MAKE2</option>
</select>
<select name="model" id="model">
<option value="">ANY MODEL</option>
<option value="model1">MODEL1</option>
<option value="model2">MODEL2</option>
</select>
<select name="minprice" id="minprice">
<option value="">ANY PRICE</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select name="maxprice" id="maxprice">
<option value="">ANY PRICE</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</form>
然后在search.php页面上,我可以获得所有变量,但我正在努力编写SELECT查询。我尝试过使用AND,但当然,只有设置了所有变量才能使用。我总共有8个可以设置的变量。 我甚至试图做类似下面的事情,但显然不是要走的路。
SELECT * FROM search data where if(isset($make)) AND if(isset($model)) etc etc
非常感谢大家的帮助。任何建议将不胜感激。我尽可能多地学习,但我真的很喜欢这个。
更新:所以我做了一些创造性的实验,我觉得我很接近,只是无法让它发挥作用:
$query = array();
if (!empty($_GET['vType'])) {
$query[] = "vType='$vType'";
}
if (!empty($_GET['make'])) {
$query[] = "make='$make'";
}
if (!empty($model)) {
$query[] = "model='$model'";
}
if (!empty($yearfrom)) {
$query[] = "minyear >'$yearfrom'";
}
if (!empty($yearto)) {
$query[] = "maxyear>'$yearto'";
}
if (!empty($minprice)) {
$query[] = "minPrice >'$minprice'";
}
if (!empty($maxprice)) {
$query[] = "maxyear >'$maxprice'";
}
if (!empty($location)) {
$query[] = "location='$location'";
}
if (empty($query)){
$where = "WHERE";
}else{
$where = "";
}
$query = implode(' AND ', $query);
$sql = "SELECT * FROM searchdata $where $query";
$result = mysqli_query($conDB, $sql);
if (mysqli_num_rows($result) === 0) {
echo 'Nothing Found';
}
while ($row = $result->fetch_assoc()) {
echo $row['vType'];
我确定我在这里做了一件非常错事。谢谢你们,我非常感谢你的帮助
答案 0 :(得分:1)
您也可以这样做
$sql = "select * from table_name where column = $param1"
if(isset($param2))
{
$sql=. "and column2 = $param2"
}`
if(isset($param3))
{
$sql=. "and column3 = $param3"
}`
if(isset($param4))
{
$sql=. "and column4 = $param4"
}`
答案 1 :(得分:0)
您可以使用带有php脚本的isset并构建查询框架。像这样:
$query = "select * search_data where";
if(isset($make) && isset($model)){
//do something
$query .= "something you want to do ";
}
答案 2 :(得分:0)
经过很多个小时后才弄清楚这些人。以为我会为你发布代码,因为其他任何人都面临同样的问题。再次感谢您的回复: $ query = array();
if (!empty($_GET['vType'])) {
$vType = $_GET['vType'];
$query[] = "vType='$vType'";
}
if (!empty($_GET['make'])) {
$make = $_GET['make'];
$query[] = "make='$make'";
}
if (!empty($_GET['model'])) {
$model = $_GET['model'];
$query[] = "model='$model'";
}
if (!empty($_GET['minyear'])) {
$yearfrom = $_GET['minyear'];
$query[] = "years>='$yearfrom'";
}
if (!empty($_GET['maxyear'])) {
$yearto = $_GET['maxyear'];
$query[] = "years<='$yearto'";
}
if (!empty($_GET['minPrice'])) {
$minprice = $_GET['minPrice'];
$query[] = "vPrice>='$minprice'";
}
if (!empty($_GET['maxPrice'])) {
$maxprice = $_GET['maxPrice'];
$query[] = "vPrice<='$maxprice'";
}
if (!empty($_GET['location'])) {
$location = $_GET['location'];
$query[] = "location='$location'";
}
if (!empty($query)){
$where = "WHERE";
}else{
$where = "";
}
$searchquery = implode(' AND ', $query);
$getsearch = "SELECT * FROM searchdata $where $searchquery";
$ressearch = mysqli_query($conDB, $getsearch);
if (mysqli_num_rows($ressearch) === 0) {
echo 'Nothing Found';
}else{
while($r = $ressearch->fetch_assoc()) {
//echo results here...
}
谢谢大家:)