【问题标题】:select mysql columns with checkbox and display data in html table,php选择带有复选框的mysql列并在html表中显示数据,php
【发布时间】:2016-09-16 05:01:10
【问题描述】:

我有 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 mysql arrays multiple-columns


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

    【讨论】:

    • 使用您的方法,错误显示“数组到字符串的转换”。如何在表中显示数组数据
    【解决方案2】:

    您的列名不是逗号分隔的,因此只需遍历数组并为每个元素附加一个逗号,然后在循环结束后删除最后一个逗号

    <?php
        $check_list= $_POST['check_list']; // array
        foreach($colnames as $check_list)
        {
            $cols .= $colnames . ',';
        }
        $cols = substr($cols,0,strlen($cols)-1);
        $sql = "SELECT " . $cols . " FROM teachers";
        //rest of the code
    
    ?>
    

    【讨论】:

    • 没关系。但是如何在 html 表中显示数据呢? @Atal Kishore
    • 好的。我似乎再也没有得到任何回应。有人可以帮帮我吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 2014-08-24
    相关资源
    最近更新 更多