PHP 如果用户 selects "ALL" 选项来自 select 列表选项,如何显示来自数据库的所有结果

PHP How to show all results from db if the user selects "ALL" option from select list options

您好 :) 我有一个包含 select 列表和搜索按钮的表单,用于在 table 中显示来自数据库的结果。我所有 select 列表的第一个选项是 "ALL"(这不是来自数据库!基本上它应该在用户单击搜索时从数据库中获取所有数据而没有 selecting 任何特定选项).如果我 select 实际选项(不是第一个选项 "ALL"),当前代码工作正常。我的问题是:如果用户 selects "ALL" 在一个 select 列表中,或者在所有 select 列表中,如何获取所有数据?

<!-- 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 -->

根据我对你问题的理解,你想修改你的 $q 查询以查找被选为 "ALL" 的项目。

查询在 WHERE 部分限制了返回值的数量。因此,如果用户选择全部,只需删除该部分查询。

我会为用户名部分做,您可以对其余部分使用相同的概念:

$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",你就有问题了:-)

我没有提出解决方案,而是自己尝试了代码。我稍微修改了页面以使其在没有数据库的情况下工作。重要的是底部部分。

我去掉了table格式,我专注于根据用户的选择动态构建查询。了解:

  • 当用户选择 ALL 时,没有任何东西可以过滤,因此 $query 中甚至都没有提到它。
  • 如果用户为所有字段选择 ALL,则查询中不需要 WHERE 子句($query 保持为空)。
  • 在用户选择多个字段而不是ALL 时,每个WHERE 项必须用"AND" 分隔。所以我们必须在 $query.
  • 中添加 "AND"

所以我制作和测试的代码是这样的(最下面的部分,其余的我没改):

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

因此您的查询会动态变化。然后您可以 运行 它并显示输出。 希望这对您有所帮助!