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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化