我是 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