2016-11-04 6 views
0

私はちょうどDatatables jQuery Pluginを調べ始めました。現在の仕事でうまく動作するようになっています。PHP-AJAX:php/json配列を使用してクエリからjqueryデータ型を取り込む方法

通常、AJAXコールバックを使用してテーブルにデータを取り込み、配列から必要な値を取得し、PHPスクリプトを使用してデータベースにクエリを実行します。私がdatatablesのウェブサイトで読んだことのために、同様のことは可能ですが、私はエラーが発生しているので、誰かが私を助けてくれることを願って、これまで行ってきたことを投稿します。

これは、私がいくつかのajaxパラメータでdatabaleを呼び出す方法です。

<script> 
$(document).ready(function() { 
    $('#test_table').DataTable({ 
     "processing": true, 
     "serverSide": true, 
     "ajax": { 
      "url": "test.php", 
      "type": "POST" 
     }, 
     "columns": [ 
      { "data": "id" }, 
      { "data": "name" }, 
      { "data": "email" } 
     ] 
    }); 
}); 
</script> 

これはPHP側の外観です。

$sql = "SELECT * FROM test_table"; 
$res = mysqli_query($conn, $sql) or die("Error: ".mysqli_error($conn)); 

$columns = array(
      array('db' => $row['id'], 'dt' => 'id'), 
      array('db' => $row['name'], 'dt' => 'name'), 
      array('db' => $row['email'], 'dt' => 'email'), 
    ); 

echo json_encode($columns); 

ただし、「データが定義されていません」というエラーが表示されます。 (私はdatatablesのウェブサイトのドキュメントを読んだが、私はそれを一歩一歩踏み出したわけではなかった)私はこれを私が達成しようとしていることの参考にして使用した。Datatables Server Side POST

私はおそらくこれについて間違っている私はコードをあまり変更したくないので、私はうまくいくと思っていたアプローチを試みました。誰かが私にPHPのjson配列呼び出しからデータベースを問い合わせることでデータ型を入れる方法を教えてもらえれば、事前に感謝。

おかげで、

+0

周囲に 'array( 'data' =>/** /)'があります。 – JOUM

+0

PHP側がリモートで正しくありません。データの表示方法に関するドキュメントを確認してください – FrankerZ

答えて

1

は、あなたの入力をありがとうございました。私はそれを働かせる方法を考え出した。

jqueryコールバック内のデータテーブルにデータを送信するには、データテーブル外で独自の検索を作成できるため、テーブルになりたいと思っていました。私がそれをやったのは、データベースへのクエリを実行し、そのクエリの結果をデータテーブルに渡すajax呼び出しを実行することですが、データテーブルが受け入れる方法でデータをフォーマットする方法が問題でした。そのデータをテーブルに表示する方法を教えてください。

シンプルなAJAX呼び出しと移入のDataTableこのコードをさらに修正することができ

(私は、私の場合に行います)が、それはあなたに私がしたいと思っていたものを達成する方法のアイデアを与える必要があります行う。 (それはbtwで動作します)。

<script> 
$('document').ready(function() 
{ 
    $.ajax({ 
    type : 'POST', 
    url : 'test.php', 
    dataType: 'json', 
    cache: false, 
    success : function(result) 
     { 
      //pass data to datatable 
      console.log(result); // just to see I'm getting the correct data. 
      $('#test_table').DataTable({ 
       "searching": false, //this is disabled because I have a custom search. 
       "aaData": [result], //here we get the array data from the ajax call. 
       "aoColumns": [ 
        { "sTitle": "ID" }, 
        { "sTitle": "Name" }, 
        { "sTitle": "Email" } 
       ] //this isn't necessary unless you want modify the header 
        //names without changing it in your html code. 
        //I find it useful tho' to setup the headers this way. 
      }); 
     } 
    }); 
}); 
</script> 

test.phpを

これは私がテストのために使用される単純なバージョンです。私の実際のコードは、クエリと検索のためのいくつかの部分を持っているので、はるかに大きくなります。

<?php 

$columns = array( 
// datatable column index => database column name 
    0 => 'id', 
    1 => 'name', 
    2 => 'email', 
); 

$sql = "SELECT * FROM test_table"; 
$res = mysqli_query($conn, $sql) or die("Error: ".mysqli_error($conn)); 

while($row = mysqli_fetch_array($res)) { 
    $dataArray = array(); 

    $dataArray[] = $row["id"]; 
    $dataArray[] = $row["name"]; 
    $dataArray[] = $row["email"]; 

} 

echo json_encode($dataArray); 

?> 

これは少なくとも私のために多くのことを単純化します。私は追加のライブラリ 'ssp.class.php'を含める必要はありませんでした。私もPDOに入ることを望んでいませんでした。だからこそこれはもっと柔軟になりました。同じ事柄を達成しようとしている他の人たちにも役立つことを願っています。

-1
$columns = array(
'data'=> 
    array(
     $row[0]['id'], 
     $row[0]['name'], 
     $row[0]['email'], 
    ) 
); 

が正しいフォーマットである

UPDATE:あなたは、サーバー側の処理を使用する場合は、結果は

0

ループいけない、あなたは、サーバー側のヘルパークラスssp.class.phpを含めるとthis exampleに示すように、それを使用する必要があります。例えば

// DB table to use 
$table = 'test_table'; 

// Table's primary key 
$primaryKey = 'id'; 

// Array of database columns which should be read and sent back to DataTables. 
// The `db` parameter represents the column name in the database, while the `dt` 
// parameter represents the DataTables column identifier. In this case object 
// parameter names 
$columns = array(
    array('db' => 'id', 'dt' => 'id'), 
    array('db' => 'name', 'dt' => 'name'), 
    array('db' => 'email', 'dt' => 'email') 
); 

// SQL server connection information 
$sql_details = array(
    'user' => '', 
    'pass' => '', 
    'db' => '', 
    'host' => '' 
); 


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* If you just want to use the basic configuration for DataTables with PHP 
* server-side, there is no need to edit below this line. 
*/ 

require('ssp.class.php'); 

echo json_encode(
    SSP::simple($_POST, $sql_details, $table, $primaryKey, $columns) 
); 
+0

ありがとうGyrocode.com。私は私の元の記事で私がそれを終えたと言いましたが、私はそれをやりたいとは思っていませんでした。私はそれをtho '考え出した。私は答えを投稿しました。 – BlueSun3k1

関連する問題