2013-01-16 113 views
0

編輯:JSON數據的職位,但表中沒有得到填充

  1. 改變了jQuery代碼重新對飛行仍是沒有去
  2. 重組我的表仍然是一個沒有去。

我有一個PHP腳本,將我的數據庫表編碼成一個數組。我回應json_encode,它回聲就好了。腳本:

<?php 
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="testdbpass"; // Mysql password 
$db_name="test"; // Database name 

// Connect to server via PHP Data Object 
$dbh = new PDO("mysql:host=localhost;dbname=test", $username, $password); 
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

$array = $dbh->query("SELECT id, anum, first, last, 
         why, comments, aidyear, additional_req, 
         signintime FROM inoffice WHERE 
         counselorname IS NULL")->fetchAll(PDO::FETCH_ASSOC); 
echo json_encode($array); 

?> 

然後,我有一個頁面,應該提取數據,然後將其放置到一個表中。出於某種原因,我無法將其發佈到我的第一張表格上。

這些都是我已經在網頁上運行我的jQuery腳本:

<head> 
    <script src="core/media/js/jquery.js" type="text/javascript"></script> 
    <script src="core/media/js/jquery.dataTables.js" type="text/javascript"></script> 
    <script type="text/javascript" src="core/media/js/install.js"></script> 
    <script type="text/javascript"> 
    $.ajax({ 
    url: 'core/media/js/getdatainoffice.php', 
    type: 'GET', 
    async: false, 
    dataType: 'json', 
    success: function (result) { 

     var insert = ''; 

     $.each(result, function() { 
      insert += '<tr><td>' + id + '</td><td>' + anum + '</td><td>' + first + '</td><td>' + last + '</td><td>' + why + 
         '</td><td>' + comments + '</td><td>' + additional_req + '</td><td>' + aidyear + '</td><td>' 
         + signintime + '</td></tr>'; 
     }); 
     $('#datatables tr').after(insert); 
    } 


}); 

    </script>  

我需要建立在我的jQuery AJAX的數組,每個表中的數據或從我的理解,我認爲JSON處理方式數組被編碼。

這是我的表:

table id='datatables' class='display'> 
       <thead> 
        <tr> 
       <th>Session ID </th>       
       <th>A Number</th> 
         <th>First Name</th> 
         <th>Last Name</th> 
         <th>Reason for visit</th> 
         <th>Comments</th> 
         <th>Aid Year</th> 
         <th>Additional Requirements</th> 
         <th>Signin Time</th> 
        </tr> 
       </thead> 
        <tbody> 


       </tbody> 
     </table> 

任何幫助或解釋將是可愛。我一直在讀這個site,但無濟於事。

JSON數組:

[{ 「ID」: 「7」, 「ANUM」: 「B00000000」, 「第一」: 「rixhers」, 「最後一個」: 「ajazi」, 「爲什麼」 :「其他」,「評論」:「Jut 需要一些 幫助!!!」,「additional_req」:「」,「aidyear」:「12-13」,「signintime」:「2013-01-16 09 :08:35 「},{」 ID 「:」 8" , 「ANUM」: 「A00000000」, 「第一」: 「rixhers」, 「最後的」: 「ajazi」, 「爲什麼」: 「上訴」,「評論「:」「additional_req」:「」,「aidyear」:「12-13」,「signintime」:「2013-01-16 09:28:57」},{「id」:「9」, 「ANUM」: 「A00000000」, 「第一」: 「rixhers」, 「最後的」: 「ajazi」, 「爲什麼」: 「上訴」, 「評論」: 「」, 「additional_req」: 「」, 「aidyear」: 「12-13」,「signintime」:「2013-01-16 10:12:07」},{「id」:「10」 「ANUM」: 「A00000000」, 「第一」: 「rixhers」, 「最後的」: 「ajazi」, 「爲什麼」: 「上訴」, 「評論」: 「」, 「additional_req」: 「」, 「aidyear」 「12-13」,「signintime」:「2013年1月16日 十一點19分十八秒」}]

的更多信息:我使用所謂的DataTable一個jQuery插件,我需要填充該表。

回答

2
   $.(#datatables).append(data) 

在你運行這個的時候,data是一個數組。你不能只是將一些隨機的JavaScript數據結構附加到DOM中 - 你必須從該數組中的數據實際構建錶行。例如

html = '' 
for (x in data) { 
    html += '<tr><td>' + data['somefield'] + '</td></tr>'; 
} 
$('#datatables').append(html); 

如果你正在使用這些數據做的是將它填滿到一個HTML表,那麼它很可能是更容易建立在PHP服務器上的HTML和推它通過線路作爲一個字符串。

+0

因此,對於你在後面的評論中所說的話,你說在inofficejson.php中建立表的開始json編碼發生在哪裏? - 然後用數據數組傳輸表? – RaGe10940

+0

這取決於你。但個人而言,我討厭在javascript中構建html,並嘗試儘可能多地在服務器端完成它。 –

+0

好吧我會開始工作,並報告我所做的。非常感謝 – RaGe10940