在PHP方面经验丰富,但对jQuery不熟悉:我想用ajax从json编码的php文件中获取数据来填充一个简单的数据表。
table.php
:HTML代码段
<head>...
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('#example').dataTable({
"bProcessing": true,
"sAjaxSource": 'deliver_tests.php',
"sScrollY": "200px",
"bPaginate": false
});
} );
</script>
</head>
<body> ...
<div id="demo">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example" width="100%">
<thead>
<tr>
<th>id</th>
<th>Status</th>
<th>Abkürzung</th>
<th>Test</th>
<th>Patient</th>
<th>Datum</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
deliver_tests.php
:代码段
try {
$sql = "SELECT tsID, tsStatus, tbShortname, tbName, paCode, tsCompleted_Date FROM test LEFT JOIN testbase ON tsTestBaseID = tbID LEFT JOIN patient ON tsPatientID = paID WHERE tsAccountID=:accID ORDER BY tsCompleted_Date DESC";
$dbTests = $objDB->prepare($sql);
$bind=array('accID' => $_SESSION['aID']);
$dbTests->execute($bind);
$tests = $dbTests->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $dbe) {
// error
}
$tests = array('aaData' => $tests);
echo json_encode($tests);
在此处查看其输出:http://pastebin.com/uZiLepSb
致电table.php
,我收到 js-alert:
DataTables warning (table id = 'example'):
Requested unknown parameter '0' from the data source for row 0
我想我必须重新组合那个数组? 我需要提示如何从这里继续 - 谢谢你提前!
答案 0 :(得分:1)
每个表行都需要是一个数组而不是JSON中的对象,您应该可以使用PDO::FETCH_NUM
代替PDO::FETCH_ASSOC
来执行此操作。