2013-04-15 26 views
0

在PHP中有經驗,但對jQuery來說是新的:我想用ajax從json編碼的php文件中獲取數據來填充簡單的數據表。PHP:提供用於填充數據表的JSON

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-警報:

DataTables warning (table id = 'example'): 
Requested unknown parameter '0' from the data source for row 0 

我想我必須要重構的數組?
我需要提示如何從這裏繼續 - 提前謝謝!

回答

1

每個表格行都需要是一個數組而不是JSON中的對象,您應該可以使用PDO::FETCH_NUM而不是PDO::FETCH_ASSOC來完成此操作。

參見:DataTables AJAX source example

+0

是的!這就是訣竅! – michi