php - 选择带有复选框的 mysql 列并在 html 表、php 中显示数据

标签 php mysql arrays multiple-columns

我有 5 个月的 php 和 mysql 工作经验。我的问题是如何使用复选框选择 mysql 数据库表中的某些列,并在 php 的 html 表中显示这些选定列的数据? 请参阅下面的代码并帮助我解决这个问题。

复选框代码:

<form role="form" id="viewallteachers" method="POST" autocomplete="on" action="">
                    <h4>Kindly check the items to view in the table. You have a maximum of 10 items to view at a time in the table.</h4>
                    <hr>
                    <div class="row">
                        <div class="col-sm-4">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" name="check_list[]" value="tpicture">Picture
                                </label>
                            </div>
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" name="check_list[]" value="teacherID">Teacher ID
                                </label>
                            </div>
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" name="check_list[]" value="staffID">
                                    Staff ID
                                </label>
                            </div>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" name="btnshowcolumns" class="btn btn-primary">Save changes</button>
                    </form>

下面是我的表格代码,用于显示所选列和每个所选列的数据。

<?php  if(!empty($_POST['check_list'])) {
 $check_list = $_POST['check_list']; 
 $counter = count($check_list); 
 for($x=0; $x<=$counter; $x++){ 
   if(!empty($check_list[$x])){ 
    $sql = "SELECT `".$check_list[$x]."` FROM teachers";
$db = new PDO('mysql:host=localhost:3306;dbname=gnoc_db', 'root', 'xxxx');
       $select = $db->prepare($sql);
       $select->execute();
       while($row = $select->fetch(PDO::FETCH_ASSOC)) {
       echo '<tr>';
       foreach ($row as $key=>$value) {
            echo '<td>'.$key . ' = ' . $value .'</td>';
               }
               echo '</tr>';
                } /* END OF IF */
                 } /* END OF FOR LOOP */
        }
     }
?>
</tbody>
</table>

下面是图片: image of checkbox selecting the database columns image of table show data

最佳答案

<?php
    $columns = $_POST['check_list']; // array
    foreach($columns as $column)
    {
        $cols .= $column . ',';
    }
    $sql = "SELECT " . $cols . " FROM teachers";
    // do you stuff

关于php - 选择带有复选框的 mysql 列并在 html 表、php 中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39523997/

相关文章:

javascript - 在一天的特定时间刷新网页

php - mysql View 或查询显示不同日期的数据?

python - 基于numpy 1D数组的2D numpy数组中的智能分配

html - 如何检索数组对象中的文本?

JavaScript - 在拼接数组中的项目然后将该数组重新呈现到我的页面时遇到问题

php - 如何在不使用 API key 的情况下获取所有谷歌字体的列表?

php - 如何过滤YouTube视频,使其仅包含具有特定时长的元素?

php - 自定义联系表单不起作用 Yii2

MySQL 与 group by 的案例

Android sqlite 查询给出列不存在的错误,而实际上该列确实存在