dongyi3776 2017-02-25 10:38
浏览 31
已采纳

使用基于数据库数据的自动填充选项过滤表

I have a table and I have a filter that currently will filter by department (only option is admin currently) and I want the filter options to generate based on all the departments that are input in the database so when a new employee is added and a new department has been input then it will automatically show as an option in the filter. Is that possible?

<?php
        if(isset($_GET['aksi']) == 'delete'){
            $id = $_GET['id'];
            $cek = mysqli_query($db, "SELECT * FROM employees WHERE id='$id'");
            if(mysqli_num_rows($cek) == 0){
                echo '<div class="alert alert-info alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>Data not found</div>';
            }else{
                $delete = mysqli_query($db, "DELETE FROM employees WHERE id='$id'");
                if($delete){
                    echo '<div class="alert alert-primary alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> Employee removed</div>';
                }else{
                    echo '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> Employee removal failed</div>';
                }
            }
        }
        ?>

        <form class="form-inline" method="get">
        <a href="add.php" title="Add" class="btn btn-primary-add btn"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span></a>
            <div class="form-group">
                <select name="filter" class="form-control" onchange="form.submit()">
                    <option value="0">Filter By Department</option>
                    <?php $filter = (isset($_GET['filter']) ? strtolower($_GET['filter']) : NULL);  ?>
                    <option value="Admin" <?php if($filter == 'Admin'){ echo 'selected'; } ?>>Admin</option>
                </select>
            </div>
        </form>
        <br />
        <div class="table-responsive">
        <table class="table table-striped table-hover">
            <tr>
                <th>ID</th>
                <th></th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>D.O.B</th>
                <th>Telephone</th>
                <th>E-mail</th>
                <th>Job Title</th>
                <th>Site</th>
                <th>Department</th>
                <th>Tools</th>
            </tr>
            <?php
            if($filter){
                $sql = mysqli_query($db, "SELECT * FROM employees WHERE department='$filter' ORDER BY last_name ASC");
            }else{
                $sql = mysqli_query($db, "SELECT * FROM employees ORDER BY last_name ASC");
            }
            if(mysqli_num_rows($sql) == 0){
                echo '<tr><td colspan="8">Data Tidak Ada.</td></tr>';
            }else{
                $no = 1;
                while($row = mysqli_fetch_assoc($sql)){
                    echo '
                    <tr>
                        <td>'.$row['id'].'</td>
                        <td><a href="details.php?id='.$row['id'].'"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></td> 
                        <td>'.$row['first_name'].'</a></td>
                        <td>'.$row['last_name'].'</td>
                        <td>'.$row['dob'].'</td>
                        <td>'.$row['telephone'].'</td>
                        <td>'.$row['email'].'</td>
                        <td>'.$row['job_title'].'</td>
                        <td>'.$row['site'].'</td>
                        <td>'.$row['department'].'</td>
                        <td>
                            <a href="details.php?id='.$row['id'].'" title="View" class="btn btn-primary-view btn-sm"><span class="glyphicon glyphicon-unchecked" aria-hidden="true"></span></a>
                            <a href="edit.php?id='.$row['id'].'" title="Edit" class="btn btn-primary btn-sm"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span></a>
                            <a href="password.php?id='.$row['id'].'" title="Change Password" data-placement="bottom" data-toggle="tooltip" class="btn btn-warning btn-sm"><span class="glyphicon glyphicon-refresh" aria-hidden="true"></span></a>
                            <a href="index.php?aksi=delete&id='.$row['id'].'" title="Delete" onclick="return confirm(\'Are you sure you want to delete '.$row['first_name'].'?\')" class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
                        </td>
                    </tr>
                    ';
                    $no++;
                }
            }
            ?>
  • 写回答

1条回答 默认 最新

  • duannao3819 2017-02-25 11:45
    关注

    You should use jquery datatable plugin for adding lots of stuffs with your table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程