按类别过滤以在php中打印

时间:2018-01-12 09:24:12

标签: php sql

我正在制作一份报告,我需要按类别过滤细节。我的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 table

这是我想要的输出:

output

2 个答案:

答案 0 :(得分:1)

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的开放式代码。防止使用prepared statements

<强> 参考: -

mysqli prepared statements

PDO prepared statements

答案 1 :(得分:0)

虽然我更喜欢AliveToDie的解决方案,但我想给你一个不会刷新类别选择页面的替代方案。

当您更改类别选择的值时,可以使用javascript显示和隐藏列 为此,你需要给tr一个类别名为

的类
<!-- table with id for easy js  -->
<table id="itr_table">

<!-- table headers... -->

<!-- table body -->
<tr class="tr_body tr_<?php echo $fetch['category'] ?>">
  <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>


<!-- category select, I added a changelistener -->
<select style = "width:22%;" class = "form-control" name = "category" required = "required" onchange="filterByCategory(this)">
    <option value = "">--Select category--</option>
    <option value = "1">RESIDENT</option> <!-- the value should be the actual id of the category. -->
    <option value = "2">4Ps</option>
</select>

<!-- javascript -->
<script>
filterByCategory(selectElement){
    category = $(selectElement).value();

    //hide all trs
    $('#itr_table .tr_body').hide();

    //show trs with chosen category
    $('#itr_table .tr_'.category).show();
}
</script>

请注意,这未经过测试,可能会出现一些错误。我只想告诉你如何处理这个问题。