我正在制作一份报告,我需要按类别过滤细节.我的itr表中有两个类别,即Resident和4Ps.
到目前为止,这是我的代码.我可以在这里打印itr表的所有细节.
<table>
<tr>
<th style = "padding-right:80px;padding-left:150px;">
<center>Name</center></th>
<th style = "padding-right:10px;padding-left:15px;"><center>Age</center>
</th>
<th style = "padding-right:10px;padding-left:20px;">
<center>Gender</center></th>
<th style = "padding-right:30px;padding-left:40px;">
<center>Purok</center></th>
</tr>
<?php
$query = $conn->query("SELECT * FROM itr LIMIT 30") or
die(mysqli_error());
for($a = 1; $a <= 30; $a++){
$fetch = $query->fetch_array()
?>
<tr>
<td><?php echo $a.". ".$fetch['firstname']." ".$fetch['firstname']?></td>
<td><center><?php echo $fetch['age']?></center></td>
<td><center><?php echo $fetch['gender']?></center></td>
<td><center><?php echo $fetch['address']?></center></td>
</tr>
<?php
}
$conn->close();
?>
我想要的是只使用像“按类别过滤”这样的按钮打印4P类别的人,我可以选择按居民或4P过滤,这样我只能使用一个php文件.我不知道怎么做.请帮忙 :)
这是类别的html
<label for = "category">Please select category:</label>
<select style = "width:22%;" class = "form-control" name = "category" required = "required">
<option value = "">--Select category--</option>
<option value = "RESIDENT">RESIDENT</option>
<option value = "4Ps">4Ps</option>
</select>
<br />
这是我的itr表:
这是我想要的输出:
最佳答案 1.您需要在表单中放置category-select-box
2.当用户根据即将到来的类别提交表单时,需要更改查询.
如下所示: –
<form method="POST">
<label for = "category">Please select category:</label>
<select style = "width:22%;" class = "form-control" name = "category" required = "required">
<option value = "">--Select category--</option>
<option value = "RESIDENT">RESIDENT</option>
<option value = "4PS">4Ps</option> <!-- changed option value to capitals-->
</select>
<input type="submit" name="submit" value="submit">
</form>
<table>
<tr>
<th style = "padding-right:80px;padding-left:150px;">
<center>Name</center></th>
<th style = "padding-right:10px;padding-left:15px;"><center>Age</center>
</th>
<th style = "padding-right:10px;padding-left:20px;">
<center>Gender</center></th>
<th style = "padding-right:30px;padding-left:40px;">
<center>Purok</center></th>
</tr>
<?php
if(isset($_POST['category']) && $_POST['category'] !==''){
$category = $_POST['category'];
$query = $conn->query("SELECT * FROM itr where category = '$category' LIMIT 30") or die(mysqli_error());
}else{
$query = $conn->query("SELECT * FROM itr LIMIT 30") or die(mysqli_error());
}
for($a = 1; $a <= 30; $a++){
$fetch = $query->fetch_array()
?>
<tr>
<td><?php echo $a.". ".$fetch['firstname']." ".$fetch['firstname']?></td>
<td><center><?php echo $fetch['age']?></center></td>
<td><center><?php echo $fetch['gender']?></center></td>
<td><center><?php echo $fetch['address']?></center></td>
</tr>
<?php
}
$conn->close();
?>
注意: – 对于SQL INJECTION,您的代码是敞开的.防止它使用准备好的陈述
参考:-