我是php和sql的新手,所以请耐心等待。我确实尝试过搜索,但没找到相当于我正在寻找的。希望你能帮助我。所以这里去了!
我有一定数量的带有值的下拉菜单。我想包括一个select all选项,它将选择所有选项并对所有值执行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 EXCLUDE $ dist
答案 0 :(得分:1)
您可以按name='filter1[]'
您可以按$filter1= serialize($_POST['filter1']) ;
答案 1 :(得分: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);
答案 2 :(得分: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>
快乐编码:)