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

时间:2017-02-25 10:38:58

标签: php mysql ajax

我有一个表,我有一个过滤器,目前将按部门过滤(目前只有管理员选项)我想要根据数据库中输入的所有部门生成过滤器选项,这样当新员工是添加了一个新的部门,然后它会自动显示为过滤器中的一个选项。这可能吗?

<?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 个答案:

答案 0 :(得分:0)

您应该使用jquery datatable plugin在桌面上添加大量内容。