【发布时间】:2017-10-03 23:17:21
【问题描述】:
嗨 :) 我有一个包含选择列表和搜索按钮的表单,用于在表中显示来自数据库的结果。我所有选择列表的第一个选项是“全部”(这不是来自数据库!基本上它假设当用户单击搜索而不选择任何特定选项时,它会从数据库中获取所有数据)。如果我选择真实选项(不是第一个选项“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 -->
【问题讨论】:
标签: php mysql select search selectlist