0

编辑:

  1. 更改了 Jquery 代码以动态重新创建行仍然不行
  2. 重组我的桌子仍然不行。

我有一个 php 脚本,可以将我的 db 表编码为一个数组。我回显了 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>

任何帮助或解释都会很可爱。我一直在阅读这个网站,但无济于事。

JSON数组:

[{"id":"7","anum":"B00000000","first":"rixhers","last":"ajazi","why":"Other","comments":"需要一些帮助!!!","additional_req":"","aidyear":"12-13","signintime":"2013-01-16 09:08:35"},{"id":"8", "anum":"A00000000","first":"rixhers","last":"ajazi","why":"Appeal","comments":"","additional_req":"","aidyear": "12-13","signintime":"2013-01-16 09:28:57"},{"id":"9","anum":"A00000000","first":"rixhers","最后":"ajazi","为什么":"上诉","comments":"","additional_req":"","aidyear":"12-13","signintime":"2013-01-16 10:12:07"} ,{"id":"10","anum":"A00000000","first":"rixhers","last":"ajazi","why":"Appeal","comments":""," Additional_req":"","aidyear":"12-13","signintime":"2013-01-16 11:19:18"}]rixhers","last":"ajazi","why":"Appeal","comments":"","additional_req":"","aidyear":"12-13","signintime":"2013- 01-16 11:19:18"}]rixhers","last":"ajazi","why":"Appeal","comments":"","additional_req":"","aidyear":"12-13","signintime":"2013- 01-16 11:19:18"}]

更多信息:我正在使用一个名为 datatables 的 jquery 插件,我需要填充该表。

4

1 回答 1

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 并将其作为单个字符串推送到网络上可能会更容易。

于 2013-01-16T14:44:50.257 回答