嗨:)我有一个包含选择列表和搜索按钮的表单,以显示表中数据库的结果。我所有选择列表的第一个选项是" ALL" (这不是来自数据库!基本上,它假设在用户单击“搜索”而不选择任何特定选项时从数据库中提取所有数据)。如果我选择实际选项(不是第一个选项" ALL"),当前代码工作正常。我的问题是:如果用户选择" ALL"在一个选择列表中,还是在所有选择列表中?
<!-- Search Components -->
<div class="row">
<form action="search.php" method="POST" name="" class="text-center">
<!-- Username -->
<div class="form-group col-sm-6 col-md-4">
<?php
include("dbc.php");
$qq = "SELECT DISTINCT username FROM users ORDER BY username";
$rr = mysqli_query($dbc,$qq);
while ($row = mysqli_fetch_array($rr)){
$username_array[] = $row['username'];
}
echo '<select class="form-control border-input" name="username">';
echo '<option value="all">ALL</option>';
foreach($username_array as $user){
$selected = '';
if($_POST['username'] == $user) {
$selected = 'selected="selected"';
}
echo '<option value="'.$user.'"'.$selected.'>'.$user.'</option>';
}
echo '</select>';
?>
</div>
<!-- Hospital Sections -->
<div class="form-group col-sm-6 col-md-4">
<?php
include("dbc.php");
$qqq = "SELECT DISTINCT section_name FROM hospital_sections ORDER BY section_name";
$rrr = mysqli_query($dbc,$qqq);
while ($row = mysqli_fetch_array($rrr)){
$sections_array[] = $row['section_name'];
}
echo '<select class="form-control border-input" name="section_name">';
echo '<option value="all">ALL</option>';
foreach($sections_array as $sec){
$selected = '';
if($_POST['section_name'] == $sec) {
$selected = 'selected="selected"';
}
echo '<option value="'.$sec.'"'.$selected.'>'.$sec.'</option>';
}
echo '</select>';
?>
</div>
<!-- Room Number -->
<!-- Qeblah Status -->
<div class="form-group col-sm-6 col-md-4">
<select class="form-control border-input" name="qeblah_status">
<option <?php if($_POST['qeblah_status'] == "ALL") echo 'selected="selected"'; ?> value="all">ALL</option>
<option <?php if($_POST['qeblah_status'] == "yes") echo 'selected="selected"'; ?> value="yes">Yes</option>
<option <?php if($_POST['qeblah_status'] == "no") echo 'selected="selected"'; ?> value="no">No</option>
</select>
</div>
<!-- Prayer Painting -->
<div class="form-group col-sm-6 col-md-4">
<select class="form-control border-input" name="prayer_painting">
<option <?php if($_POST['prayer_painting'] == "ALL") echo 'selected="selected"'; ?> value="all">ALL</option>
<option <?php if($_POST['prayer_painting'] == "yes") echo 'selected="selected"'; ?> value="yes">Yes</option>
<option <?php if($_POST['prayer_painting'] == "no") echo 'selected="selected"'; ?> value="no">No</option>
</select>
</div>
<!-- Fatwah -->
<div class="form-group col-sm-6 col-md-4">
<select class="form-control border-input" name="fatwa_status">
<option <?php if($_POST['fatwa_status'] == "ALL") echo 'selected="selected"'; ?> value="all">ALL</option>
<option <?php if($_POST['fatwa_status'] == "yes") echo 'selected="selected"'; ?> value="yes">Yes</option>
<option <?php if($_POST['fatwa_status'] == "no") echo 'selected="selected"'; ?> value="no">No</option>
</select>
</div>
<!-- Search Button -->
<div class="form-group col-sm-6 col-md-4">
<button type="submit" class="form-control btn btn-success btn-fill btn-wd" name="submit" value="Search"><i class="ti-search"></i> Search </button>
</div>
</form>
</div> <!-- .row Ends -->
<!-- Search Components Ends -->
<hr>
<!-- Search Results -->
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$username = $_POST['username'];
$section_name = $_POST['section_name'];
$qeblah_status = $_POST['qeblah_status'];
$prayer_painting = $_POST['prayer_painting'];
$fatwa_status = $_POST['fatwa_status'];
?>
<!-- The Table -->
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="content table-responsive table-full-width">
<table class="table table-striped table-bordered" style="border-style: hidden;">
<thead>
<th>Username</th>
<th>Section Name</th>
<th>Room Number</th>
<th>Qeblah</th>
<th>Prayer Painting</th>
<th>Fatwah</th>
<th>Condition</th>
</thead>
<tbody>
<?php
$selected_username = "";
if($username != 'All'){
$selected_username = "username = '$username'";
}
$selected_section_name = "";
if($section_name != 'All'){
$selected_section_name = "section_name = '$section_name'";
}
$selected_qeblah_status = "";
if($qeblah_status != 'All'){
$selected_qeblah_status = "qeblah_status = '$qeblah_status'";
}
$selected_prayer_painting = "";
if($prayer_painting != 'All'){
$selected_prayer_painting = "prayer_painting = '$prayer_painting'";
}
$selected_fatwa_status = "";
if($fatwa_status != 'All'){
$selected_fatwa_status = "fatwa_status = '$fatwa_status'";
}
include("dbc.php");
$q = "SELECT * FROM reports WHERE $selected_username AND $selected_section_name AND $selected_qeblah_status AND $selected_prayer_painting AND $selected_fatwa_status";
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r)){
echo "<tr>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['section_name']."</td>";
echo "<td>".$row['room_number']."</td>";
echo "<td>".$row['qeblah_status']."</td>";
echo "<td>".$row['prayer_painting']."</td>";
echo "<td>".$row['fatwa_status']."</td>";
echo "<td>".$row['report_status']."</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div> <!-- .content Ends -->
</div> <!-- .card Ends -->
</div> <!-- .col-md-12 Ends -->
</div> <!-- .row Ends -->
}
<!-- Search Results Ends -->
</div> <!-- .container-fluid Ends -->
</div> <!-- .content Ends -->
答案 0 :(得分:0)
我理解你的问题的方式,你想修改你的$ q查询选择“全部”的项目。
查询限制了WHERE部分中返回值的数量。因此,如果用户选择ALL,则只删除该部分查询。
我会为用户名部分做,你可以使用相同的概念:
$username_part = "";
if ($username != 'ALL')
{
$username_part = "username = '$username' AND";
}
$q = "SELECT * FROM reports WHERE $username_part section_name = '$section_name' AND qeblah_status = '$qeblah_status' AND prayer_painting = '$prayer_painting' AND fatwa_status = '$fatwa_status'";
这样,如果用户选择ALL,则不指定WHERE条件。你只需要把它全部拿走。如果用户选择一个值,它将根据用户的选择在WHERE中添加条件。
管理此类查询的结果留给您:-) 显然,如果用户名被命名为“ALL”,则会出现问题: - )
答案 1 :(得分:0)
而不是建议解决方案,我自己去了代码。我稍微修改了页面,使其在没有数据库的情况下工作。重要的部分是底部。
我取出了表格格式,我专注于根据用户的选择动态构建查询。要理解:
所以我制作和测试的代码就是这个(底部,我没有改变其余部分):
<div class="content table-responsive table-full-width">
<?php
$where = "";
$query = "";
if($username != 'all'){
$query = "username = '$username'";
$where = "WHERE ";
}
if($section_name != 'all'){
if (strlen($query) != 0)
{
$query = $query . " AND ";
}
$query = $query . "section_name = '$section_name'";
$where = "WHERE ";
}
if($qeblah_status != 'all'){
if (strlen($query) != 0)
{
$query = $query . " AND ";
}
$query = $query . "qeblah_status = '$qeblah_status'";
$where = "WHERE ";
}
if($prayer_painting != 'all'){
if (strlen($query) != 0)
{
$query = $query . " AND ";
}
$query = $query . "prayer_painting = '$prayer_painting'";
$where = "WHERE ";
}
if($fatwa_status != 'all'){
if (strlen($query) != 0)
{
$query = $query . " AND ";
}
$query = $query . "fatwa_status = '$fatwa_status'";
$where = "WHERE ";
}
$q = "SELECT * FROM reports $where $query";
echo "<h1>$q</h1>\n";
?>
</div>
因此您的查询会动态更改。然后,您可以运行它并显示输出。 希望这有帮助!