0

編集:

  1. オンザフライで行を再作成するようにJqueryコードを変更しましたが、まだうまくいきません
  2. テーブルを再構築してもまだうまくいきません。

私のdbテーブルを配列にエンコードする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>

どんな助けや説明も素敵です。私はこのサイトを読んでいましたが、役に立ちませんでした。

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","最後":"アアジ","why":"アピール","コメント":"","additional_req":"","aidyear":"12-13","signintime":"2013-01-16 10:12:07"} ,{"id":"10","anum":"A00000000","first":"rixhers","last":"ajazi","why":"アピール","コメント":""," additional_req":"","aidyear":"12-13","signintime":"2013-01-16 11:19:18"}]rixhers","last":"ajazi","why":"アピール","comments":"","additional_req":"","aidyear":"12-13","signintime":"2013- 01-16 11:19:18"}]rixhers","last":"ajazi","why":"アピール","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 に答える