【问题标题】:Populate data in datatable from MySQL: Php Ajax从 MySQL 填充数据表中的数据:Php Ajax
【发布时间】:2020-11-19 17:04:03
【问题描述】:

我正在尝试从 MySQL 数据库中检索数据并将其推送到数据表中,但数据表中没有填充数据。

当我在控制台记录数据时,我能够在后端获取数据,但我无法将其推送到数据表中。

如有任何帮助,将不胜感激

index.php

<!DOCTYPE html>
<html>
    <head>
        <script language="JavaScript" src="https://code.jquery.com/jquery-3.5.1.js" type="text/javascript"></script>
        <script language="JavaScript" src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js" type="text/javascript"></script>
        <script language="JavaScript" src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap4.min.js" type="text/javascript"></script>
        <script language="JavaScript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js" type="text/javascript"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css" type="text/css">
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap.min.css" type="text/css">
        <link rel="stylesheet" href="https://nightly.datatables.net/css/dataTables.bootstrap4.min.css" type="text/css">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    </head>
    <style>
        tfoot {
            display: table-header-group;
        }
    </style>
    <body>

        <table id="example" class="display" style="width:100%">
            <thead>
                <tr>
                    <th scope="col">control_id</th>
                    <th scope="col">sample_id</th>
                    <th scope="col">sample_nm</th>
                    <th scope="col">sample_type_nm</th>
                    <th scope="col">sample_type_with_nm</th>
                    <th scope="col">variant_type</th>
                    <th scope="col">output_location</th>
                    <th scope="col">object_type</th>
                    <th scope="col">output_type</th>
                    <th scope="col">oncotree_nm</th>
                    <th scope="col">status</th>
                    <th scope="col">pipeline_status</th>
                    <th scope="col">oncsuite_status</th>
                    <th scope="col">control_completion_status</th>
                    <th scope="col">send_oncsuite_status</th>
                    <th scope="col">api_log_creation_dt</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
            <tfoot>
            <tr>
                <th>control_id</th>
                <th>sample_id</th>
                <th>sample_nm</th>
                <th>sample_type_nm</th>
                <th>sample_type_with_nm</th>
                <th>variant_type</th>
                <th>output_location</th>
                <th>object_type</th>
                <th>output_type</th>
                <th>oncotree_nm</th>
                <th>status</th>
                <th>pipeline_status</th>
                <th>oncsuite_status</th>
                <th>control_completion_status</th>
                <th>send_oncsuite_status</th>
                <th>api_log_creation_dt</th>
            </tr>
            </tfoot>
        </table>

        <script>
            $(document).ready(function() {
                $('#example').DataTable( {
                    "processing": true,
                    "serverSide": true,
                    "ajax": {
                        "url": "../model/getData.php",
                        "type": "POST"
                    },
                    "columns": [
                        {data: 'control_id'},
                        {data: 'sample_id'},
                        {data: 'sample_nm'},
                        {data: 'sample_type_nm'},
                        {data: 'sample_type_with_nm'},
                        {data: 'variant_type'},
                        {data: 'output_location'},
                        {data: 'object_type'},
                        {data: 'output_type'},
                        {data: 'oncotree_nm'},
                        {data: 'status'},
                        {data: 'pipeline_status'},
                        {data: 'oncsuite_status'},
                        {data: 'ontrol_completion_status'},
                        {data: 'send_oncsuite_status'},
                        {data: 'api_log_creation_dt'}
                    ]
                } );

            } );
                
        </script>
    </body>

</html>

getData.php

<?php
include('connection.php');

class getData
{
    public function selectData()
    {
        $output = array();
        $pdo = Db::getInstance();
        $query = "SELECT control_id, sample_id, sample_nm, sample_type_nm, sample_type_with_nm, variant_type, output_location, object_type, output_type, oncotree_nm, status, pipeline_status, oncsuite_status, control_completion_status, send_oncsuite_status, api_log_creation_dt FROM vonc_wes.view_control_variant_context_new";
        $stmt = $pdo->prepare($query);
        try {
            $stmt->execute();
        } catch(PDOException $e) {
            echo $e;
        }
        $rows = $stmt->fetchAll();
        foreach ($rows as $row) {

            $subarray = array();
            $subarray["control_id"] = $row[0];
            $subarray["sample_id"] = $row[1];
            $subarray["sample_nm"] = $row[2];
            $subarray["sample_type_nm"] = $row[3];
            $subarray["sample_type_with_nm"] = $row[4];
            $subarray["variant_type"] = $row[5];
            $subarray["output_location"] = $row[6];
            $subarray["object_type"] = $row[7];
            $subarray["output_type"] = $row[8];
            $subarray["oncotree_nm"] = $row[9];
            $subarray["status"] = $row[10];
            $subarray["pipeline_status"] = $row[11];
            $subarray["oncsuite_status"] = $row[12];
            $subarray["ontrol_completion_status"] = $row[13];
            $subarray["send_oncsuite_status"] = $row[14];
            $subarray["api_log_creation_dt"] = $row[15];
            array_push($output, $subarray);
        }
        $outputJson = json_encode($output);
        echo $outputJson;
    }
}

$getallData             = new getData();
$getallData->selectData();

getData.php 的输出是

[{"control_id":"1","sample_id":"8043","sample_nm":"ST","sample_type_nm":"NORMAL","sample_type_with_nm":":NORMAL","variant_type":"gatk4","output_location":"atic.tsv","object_type":"tsv","output_type":"gatk4","oncotree_nm":"NORMA","status":"2","pipeline_status":"2","oncsuite_status":"0","ontrol_completion_status":"0","send_oncsuite_status":"400","api_log_creation_dt":"2020-04-1"},{"control_id":"12","sample_id":"843","sample_nm":"ST","sample_type_nm":"NORMAL","sample_type_with_nm":":NORMAL","variant_type":"gatk4","output_location":"atic.tsv","object_type":"tsv","output_type":"gatk4","oncotree_nm":"NORMA","status":"2","pipeline_status":"2","oncsuite_status":"0","ontrol_completion_status":"0","send_oncsuite_status":"400","api_log_creation_dt":"2020-04-1"}]

【问题讨论】:

    标签: php mysql datatables


    【解决方案1】:

    来自the DataTables documentation

    请注意,DataTables 期望表数据是对象的 data 参数中的项目数组(如果您的数据格式不同,请使用 ajax 的 ajax.dataSrc 选项作为对象)

    您发回的对象只是一个数组,而不是具有data 属性的对象。最简单的方法可能是使用that ajax.dataSrc option 并将其设置为空字符串:

    "ajax": {
      "url": "../model/getData.php",
      "type": "POST",
      "dataSrc": ""
    }
    

    或者,您希望修改您的 PHP 代码以创建一个具有 data 属性的对象,并将您的 $output 设置为该属性的值,然后将 json_encode 并输出该对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-19
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 2016-04-23
      • 2013-04-04
      相关资源
      最近更新 更多