加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 动态获取数据表中$aColumns arrary中的列名

发布时间:2020-12-13 16:20:06 所属栏目:PHP教程 来源:网络整理
导读:首先,我会提到我想要实现的目标.我正在使用 PHP的CodeIgniter框架.我的数据库中有5个表,我希望在显示页面上单击按钮时以数据表格式显示它们.我使用服务器端处理php作为数据源.所以一开始我创建的代码只显示一个Datatable格式的表,并且成功了.现在我想在按钮
首先,我会提到我想要实现的目标.我正在使用 PHP的CodeIgniter框架.我的数据库中有5个表,我希望在显示页面上单击按钮时以数据表格式显示它们.我使用服务器端处理php作为数据源.所以一开始我创建的代码只显示一个Datatable格式的表,并且成功了.现在我想在按钮点击事件中一次显示一个表格.但是$aColumns长度应该等于HTML表中定义的列数.现在考虑标记tabe,它有4列student_id,exam_id,subject_id和marks_achieved.现在另一个表是分支,只有2列branch_id和branch_name.所以我不能动态地增加或减少HTML中的标签,所以我很困惑.
我也使用这个 source来创建数据表.
你可以查看我的getTable()函数 here.

jQuery的:

$(document).ready(function()
{
$('#datatable').dataTable({
    "sPaginationType":"full_numbers","bJQueryUI":true,"bProcessing": true,"bServerSide": true,"sServerMethod": "GET","sAjaxSource": "datatable/getTable","iDisplayStart": 0,"iDisplayLength": 10,"aLengthMenu": [[10,25,50,-1],[10,"All"]],"aaSorting": [[0,'asc']],"aoColumns": [
        { "bVisible": true,"bSearchable": true,"bSortable": true },{ "bVisible": true,"bSortable": true }
        ]
})

$('input[type=button]').bind('click',function(){
        var param = $(this).attr('id');
            data = param + '=1';

        $.ajax({
            type: 'POST',url: 'datatable',data: data                
        }).done(function( data ) { 
            console.log(data);
            $('#display_area').html(data); 
        });
    })
    });

HTML:

<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../js/javascript.js"></script>
<script type="text/javascript" src="../js/jquery.dataTables.min.js"></script>
</head>
<body id="dt_example">
<form action="" method="post">
<input type="button" id="display_branch" name="display_branch" value="Display Branch Table" >
<input type="button" id="display_marks" name="display_marks" value="Display Marks Table" >
</form>   
<div id="container">
<div id="demo">
<table id="datatable" cellpadding="0" cellspacing="0" border="0" width="100%">
<thead>
    <tr>
        <th>Student ID</th>
        <th>Exam ID</th>
        <th>Subject ID</th>
        <th>Marks Achieved</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
</tbody>
<tfoot></tfoot>
</table>
</div>
<div class="spacer"></div>
</div>
</body>
</html>

为了动态获取列,我已经在datatable.php中进行了此更改,但它无法正常工作.这里有什么问题,或者我应该尝试其他方法?

if(isset($_POST['display_marks']))
    {
        $aColumns = array('student_id','exam_id','subject_id','marks_achieved');
        $sTable = 'marks';
    }
    if(isset($_POST['display_branch']))
    {
        $aColumns = array('branch_id','branch_name');
        $sTable = 'branch';
    }

编辑:
user1190992发布的解决方案有效,但整个方法都有所改变.在那里我想清理列的标题.显示“branch_id”而不是我想显示分支ID.我该如何进行这种消毒?

解决方法

这是一种从JSON数据动态创建HTML的非常简单的方法.但它不使用服务器端处理.

JavaScript的

$(document).ready(function() {
    $(".abutton").click(function() {
        $('#myDatatable_wrapper').detach(); //Remove existing table
        var table = '<table id="myDatatable" class="table"><thead><tr>';
        $.ajax({
            url: 'dt.php',data: "table_id="+$(this).attr("id"),type: "POST",success: function (data) {
                $.each(data.aoColumns,function(key,value) {
                    table += "<th>"+value+"</th>";
                });
                table += "</tr></thead><tbody>";
                $.each(data.aaData,row) {
                    table += "<tr>";
                    $.each(row,fieldValue) {
                        table += "<td>"+fieldValue+"</td>";
                    });
                    table += "</tr>";
                });
                table += '<tbody></table>';
                $('.container').html(table);
                $('#myDatatable').dataTable();
            },dataType: "json"
        });
    });
});

PHP

$table_id = filter_input(INPUT_POST,"table_id",FILTER_SANITIZE_STRING);
$dbconn = mysqli_connect("localhost","username","password");

if($table_id == "table1") {
    $sql_query = mysqli_query($dbconn,'SELECT * FROM display_branch');
}
else {
    $sql_query = mysqli_query($dbconn,'SELECT * FROM display_marks');
}

if(mysqli_num_rows($sql_query) == 0) {
    echo "Check your ID";
    exit(1);
}
$data = array();
$data['aaData'] = array();
while($row = mysqli_fetch_assoc($sql_query)) {
    $data['aaData'][] = $row;
}

$data['aoColumns'] = array();
while($finfo = mysqli_fetch_field($sql_query)) {
    $data['aoColumns'][] = $finfo->name;
}
echo json_encode($data);

HTML

<button id="table1" class="abutton">Table 1</button><br /><button id="table2" class="abutton">Table 2</button>
<div class="container"></div>

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读