我有6个<select>
盒子。我正在根据<select>
框做报告。我所做的是制作一个表单并将方法设置为 GET,然后在下一页上通过 $_GET 方法获取这些值。喜欢
<form method="get" action="report.php">
<select name="select1">
<option value="none">Select value</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="select2">
<option value="none">Select value</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="select3">
<option value="none">Select value</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
.....................
</form>
这是PHP代码。(这些都是示例数据:P(只是想告诉你人们我的问题。我正在检查所有值是否等于无值并执行此操作,等等。
if(
$cby == "none"
&& $crequirment == "none"
&& $cdepartment == "none"
&& $period == "none"
&& $ccontact == "none"
&& $cattend == "none"
)
{
$query = mysqli_query($db, "SELECT * FROM customer");
}
else if (
$cby != "none"
&& $crequirment == "none"
&& $cdepartment == "none"
&& $period == "none"
&& $ccontact == "none"
&& $cattend == "none"
)
{
$query = mysqli_query($db, "SELECT * FROM customer WHERE cby = $cby");
}
else if (
$cby == "none"
&& $crequirment == "none"
&& $cdepartment != "none"
&& $period == "none"
&& $ccontact == "none"
&& $cattend == "none")
{
$query = mysqli_query($db, "SELECT * FROM customer WHERE cdepartment = $cdepartment");
}
else if..........
我想要的是尽可能简单。我应该采用什么方法?
优化您的代码。
创建名称为 data[cby]
、 data[crequirment]
的选择。
代替none
使用空字符串。
在 PHP 中:
if (isset($_GET['data']) {
$where = [];
foreach ($_GET['data'] as $name => $value) {
if ($value != '') {
$where[] = $name."=".$value; // sanitize `$name` and `$value` first
}
}
if (!empty($where)) {
$whereString = ' WHERE '.implode(' AND ', $where);
} else {
$whereString = '';
}
$query = mysqli_query($db, "SELECT * FROM customer".$whereString);
}