如何将JSON编码的PHP数组发送到同一页面上的数据表 [英] How to send JSON encoded PHP array to datatables on same page

查看:38
本文介绍了如何将JSON编码的PHP数组发送到同一页面上的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个简单的问题,但它是数据表的新手,所以请原谅我的无知:我有一个查询,查询了放置在数组中然后以JSON格式编码的行,如何将该数组发送到数据表以进行填充每一行?我看过其他线程,但是它们使用ajax外部引用脚本,而我的线程位于同一页面上(不确定这是否有所不同).我的脚本如下:

Have a simple question here but am new to datatables so forgive my ignorance: I have a query of rows that I placed in an array and then encoded it in JSON format, how can I send this array to datatables in order to populate each row? I've looked at other threads but they are using ajax to externally reference the script whereas mine is located on the same page (not sure if that makes a difference). My script looks like this:

<?php
if(isset($_POST['post_id'])){
    $in = $_POST['post_id'];
}
$data = array();
foreach ($in as $id){
    $query = $link->prepare("SELECT provider_num, provider_name, 233_net_charity_care, 291_cost_of_non_mcr_bad_debts, 301_cost_of_uncomp_care 
                            FROM `s10`
                            WHERE `id` = :id");
    $query->bindParam(':id', $id, PDO::PARAM_INT);
    $query->execute();
    $results = $query->fetch(PDO::FETCH_ASSOC);
    $data[] = $results;
}
json_encode($data);
?>

其中post_id是从上一个表单提交到页面的id值的初始数组,为简单起见,可以说我这次只提交了一行.

Where post_id is the initial array of id values submitted to the page from the previous form, for simplicity's sake lets say that I only have one row submitted this time.

JSON输出如下所示

The JSON output looks like this

[{"provider_num":"140124","provider_name":"JOHN H.STROGER JR.HOSP OF COOK CTY","233_net_charity_care":"163307737","291_cost_of_non_mcr_bad_debts":"181677291","301_cost_of_uncomp_care:" 344985028}]

然后我以什么方式初始化数据表以接收此数据

In what way do I then initialize datatables to receive this data

到目前为止,我有:

$(document).ready(function() {
    $('#example').dataTable( {
        "ajax": "",                      <== What goes here?
        "columns": [
            { "data": "provider_num" },
            { "data": "provider_name" },
            { "data": "233_net_charity_care" },
            { "data": "291_cost_of_non_mcr_bad_debts" },
            { "data": "301_cost_of_uncomp_care" }
        ]
    } );
} );

任何见识都将是很棒的,我一般来说对jquery来说还很陌生,因此很抱歉这是一个简单的问题.我试图阅读文档,但遇到了麻烦.

Any insight would be great, I'm pretty new to jquery in general so apologies if this is an easy question. I tried to read the documentation but had trouble relating to it.

预先感谢

推荐答案

第一件事是,如果您的前端脚本和服务器脚本(php)在同一页面中,则无需使用Ajax.

The first thing is, if your frontend script and server script(php) is in the same page, you don't need to use ajax.

您可以在php脚本之后简单地设置像这样的数据

you can simply set the data like this after your php script

var tableData = <?php echo json_encode($data); ?>;  

然后在数据表初始化方法中,使用此

Then in datatable initialize method, use this

$(document).ready(function() {
    $('#example').dataTable({
        "aaData": tableData,                               
        "aoColumns": [
            { "data": "provider_num" },
            { "data": "provider_name" },
            { "data": "233_net_charity_care" },
            { "data": "291_cost_of_non_mcr_bad_debts" },
            { "data": "301_cost_of_uncomp_care" }
        ]
    });
});

这篇关于如何将JSON编码的PHP数组发送到同一页面上的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆