1

我是 php 和 sql 的新手,所以请多多包涵。我确实尝试过搜索,但没有找到我正在寻找的 wat。希望你能帮助我。就这样吧!

我有一定数量的带有值的下拉菜单。我喜欢包含一个全选选项,它将选择所有选项并对所有值进行 sql 查询。我编写了一个示例代码,使用 if 条件执行查询,但 if cond 的数量根据下拉菜单的数量呈指数增长。2^n 我猜。我想知道是否有更好的方法来做到这一点。请在下面找到代码。

$sql = mysql_query("SELECT distinct `Num. of Parts` from `$verb`");
echo "<select name='filter1'>";
echo "<option value='1'> Select all </option>";
while($row = mysql_fetch_assoc($sql))
{
$filter1 = $row['Num. of Parts'];
echo "<option value='$filter1'> $filter1 </option>";
}
echo "</select>";
$sql = mysql_query("SELECT distinct `Type of positioning` from `$verb`");
echo "<select name='filter2'>";
echo "<option value='1'> Select all </option>";
while($row = mysql_fetch_assoc($sql))
{
$filter2 = $row['Type of positioning'];
echo "<option value='$filter2'> $filter2 </option>";
}
echo "</select>";
echo "<input type=\"submit\" name=\"submitC\" value=\"SUBMIT\">";

submitC 值在这里传递:

if($filter1==1 && $filter2!=1)
{$sql = mysql_query("SELECT * from `$verb` where `Num. of Parts` like '%' and `Type of positioning`='$filter2' and `Distance range`='$dist'");
}
elseif($filter1!=1 && $filter2==1)
{$sql = mysql_query("SELECT * from `$verb` where `Num. of Parts`='$filter1' and `Type of positioning` like '%' and `Distance range`='$dist'");
}
elseif($filter1==1 && $filter2==1)
{$sql = mysql_query("SELECT * from `$verb` where `Num. of Parts` like '%' and `Type of positioning` like '%' and `Distance range`='$dist'");
}
else
{
$sql = mysql_query("SELECT * from `$verb` where `Num. of Parts`='$filter1' and `Type of positioning`='$filter2' and `Distance range`='$dist'");
}

提前谢谢各位!ps 排除 $dist

4

3 回答 3

1

您可以选择多个选项name='filter1[]'

你可以存储这些$filter1= serialize($_POST['filter1']) ;

于 2012-10-09T17:23:00.427 回答
0

如果我说对了,您正在搜索这样的内容:

<?php

$mapping = array ( 'column1' => 'Num_of_Parts' , 
      'column2' => 'Type_of_positioning'
);  
    $sql = "select * from table ";
    $connector = " AND ";
    if ( isset($_POST['filters']) ) {
        $sql = $sql . "where ";
        foreach ($_POST['filters'] as $key=>$val) {
            $sql .= $mapping[$key] ." like '%"  . $val. "%'" . $connector ; 
        }

    }//end of if
    $sql.= " Distance range = 'value'";
    var_dump($sql);


?>


<html>
<body>

<form action="" method="post">
<select name = "filters[column1]">
  <option value="">Select All</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

<select name = "filters[column2]">
  <option value="">Select All</option>
  <option value="saab">Car</option>
  <option value="mercedes">Bus</option>
  <option value="audi">Cycle</option>
</select>

<input type="submit" value="submit">
</form>

</body>
</html>

笔记

  1. 始终使用mysqli_real_escape_string 或类似的东西来清理用户输入,但不要使用 mysql 系列函数,因为鼓励不要在php doc中使用它们。
  2. 出于安全目的,请勿使用列名作为元素的名称。您可以在代码中看到如何使用一个简单的技巧重新映射列名。

快乐编码:)

于 2012-10-09T18:02:18.250 回答
0

试试这样

if($filter1==1) {  if(&& $filter2!=1){
$sql = "SELECT * from `$verb` where `Num. of Parts` like '%' and `Type of positioning`='$filter2' and `Distance range`='$dist'"; }
elseif($filter2==1){$sql = "SELECT * from `$verb` where `Num. of Parts` like '%' and `Type of positioning` like '%' and `Distance range`='$dist'";  }  }

elseif($filter1!=1 && $filter2==1) { 
$sql = "SELECT * from `$verb` where `Num. of Parts`='$filter1' and `Type of positioning` like '%' and `Distance range`='$dist'";  }

else{
$sql = "SELECT * from `$verb` where `Num. of Parts`='$filter1' and `Type of positioning`='$filter2' and `Distance range`='$dist'";  }

$result = mysql_query($sql);
于 2012-10-09T17:32:30.337 回答