加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

按类别过滤以在php中打印

发布时间:2020-12-13 16:10:09 所属栏目:PHP教程 来源:网络整理
导读:我正在制作一份报告,我需要按类别过滤细节.我的itr表中有两个类别,即Resident和4Ps. 到目前为止,这是我的代码.我可以在这里打印itr表的所有细节. table tr th style = "padding-right:80px;padding-left:150px;" centerName/center/th th style = "padding-r
我正在制作一份报告,我需要按类别过滤细节.我的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表:

itr table

这是我想要的输出:

output

解决方法

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,您的代码是敞开的.防止它使用准备好的陈述

参考:-

mysqli prepared statements

PDO prepared statements

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读