2016-06-15 7 views
0

MySQLデータベースからデータを取得するPHPでJSONを通じてWebページにDataTableをレンダリングしようとしています。しかし、私はそれをどうにかしていません。JSONファイルが出力されますが(エコー)、DataTableはレンダリングされず、ページを開くとエラーが表示されます: ".... DataTable #xxxxx - 無効なJSON応答... "しかし、JSONは正しいです、私は多くのJSON Validatorsでそれをテストしました。私はそれがDataTableがそれを受け入れる方法をフォーマットしていないと思う。ここで MySQLデータベースからPHPでJSONを使ってDataTableをレンダリングする

は、私は、サーバーからになってきていますかをテストするために、(エコー)をプリントアウトのget私のJSON形式、次のとおりです。ここで

<?php 


             /* Array of database columns which should be read and sent back to DataTables. Use a space where 
             * you want to insert a non-database field (for example a counter or static image) 
             */ 
             $aColumns = array('pId', 'sName', 'lName', 'pLogUrl', 'incent', 'det_sUrl'); 

             /* Indexed column (used for fast and accurate table cardinality) */ 
             $sIndexColumn = "id"; 

             /* DB table to use */ 
             $sTable = "tableName_here"; 

             /* Database connection information */ 
             $gaSql['user']  = "userHere"; 
             $gaSql['password'] = "passHere"; 
             $gaSql['db']   = "dataBaseHEre"; 
             $gaSql['server']  = "serverHere"; 


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

             /* 
             * MySQL connection 
             */ 
             $gaSql['link'] = mysql_pconnect($gaSql['server'], $gaSql['user'], $gaSql['password'] ) or 
              die('Could not open connection to server'); 

             mysql_select_db($gaSql['db'], $gaSql['link']) or 
              die('Could not select database '. $gaSql['db']); 


             /* 
             * Paging 
             */ 
             $sLimit = ""; 
             if (isset($_GET['iDisplayStart']) && $_GET['iDisplayLength'] != '-1') 
             { 
              $sLimit = "LIMIT ".mysql_real_escape_string($_GET['iDisplayStart']).", ". 
               mysql_real_escape_string($_GET['iDisplayLength']); 
             } 


             /* 
             * Ordering 
             */ 
             if (isset($_GET['iSortCol_0'])) 
             { 
              $sOrder = "ORDER BY "; 
              for ($i=0 ; $i<intval($_GET['iSortingCols']) ; $i++) 
              { 
               if ($_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true") 
               { 
                $sOrder .= $aColumns[ intval($_GET['iSortCol_'.$i]) ]." 
                 ".mysql_real_escape_string($_GET['sSortDir_'.$i]) .", "; 
               } 
              } 

              $sOrder = substr_replace($sOrder, "", -2); 
              if ($sOrder == "ORDER BY") 
              { 
               $sOrder = ""; 
              } 
             } 


             /* 
             * Filtering 
             * NOTE this does not match the built-in DataTables filtering which does it 
             * word by word on any field. It's possible to do here, but concerned about efficiency 
             * on very large tables, and MySQL's regex functionality is very limited 
             */ 
             $sWhere = ""; 
             if ($_GET['sSearch'] != "") 
             { 
              $sWhere = "WHERE ("; 
              for ($i=0 ; $i<count($aColumns) ; $i++) 
              { 
               $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch'])."%' OR "; 
              } 
              $sWhere = substr_replace($sWhere, "", -3); 
              $sWhere .= ')'; 
             } 

             /* Individual column filtering */ 
             for ($i=0 ; $i<count($aColumns) ; $i++) 
             { 
              if ($_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '') 
              { 
               if ($sWhere == "") 
               { 
                $sWhere = "WHERE "; 
               } 
               else 
               { 
                $sWhere .= " AND "; 
               } 
               $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' "; 
              } 
             } 


             /* 
             * SQL queries 
             * Get data to display 
             */ 
             $sQuery = " 
              SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))." 
              FROM $sTable 
              $sWhere 
              $sOrder 
              $sLimit 
             "; 
             $rResult = mysql_query($sQuery, $gaSql['link']) or die(mysql_error()); 

             /* Data set length after filtering */ 
             $sQuery = " 
              SELECT FOUND_ROWS() 
             "; 
             $rResultFilterTotal = mysql_query($sQuery, $gaSql['link']) or die(mysql_error()); 
             $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal); 
             $iFilteredTotal = $aResultFilterTotal[0]; 

             /* Total data set length */ 
             $sQuery = " 
              SELECT COUNT(".$sIndexColumn.") 
              FROM $sTable 
             "; 
             $rResultTotal = mysql_query($sQuery, $gaSql['link']) or die(mysql_error()); 
             $aResultTotal = mysql_fetch_array($rResultTotal); 
             $iTotal = $aResultTotal[0]; 


             /* 
             * Output 
             */ 
             $output = array(

              "aaData" => array() 
             ); 

             while ($aRow = mysql_fetch_array($rResult)) 
             { 
              $row = array(); 
              for ($i=0 ; $i<count($aColumns) ; $i++) 
              { 
               if ($aColumns[$i] == "version") 
               { 
                /* Special output formatting for 'version' column */ 
                $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ]; 
               } 
               else if ($aColumns[$i] != ' ') 
               { 
                /* General output */ 
                $row[] = $aRow[ $aColumns[$i] ]; 
               } 
              } 
              $output['aaData'][] = $row; 
             } 

             $output = str_replace("\\/", "/", $output); 

             echo json_encode($output, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); 


            ?> 

:それはこのPHPから生成され

{ 
    "aaData":[ 
     [ 
     "id12345", 
     "bchange122", 
     "textHEre", 
     "https://url.here.com/images/ppp.png!", 
     "hellooooo", 
     "https://kbkbkbk.hello.com/subid=[usr.nr]-[b_tt]|[ba_ba]|[category]" 
     ], 
     [ 
     "id23456", 
     "test122", 
     "test2HEre", 
     "https://url.here.com/images/ppp.png!", 
     "huhuhu", 
     "https://kbkbkbk.hello.com/subid=[usr.nr]-[b_tt]|[ba_ba]|[category]" 
     ], 
     [ 
     "id34567", 
     "test233", 
     "test344HEre", 
     "https://url.here.com/images/ppp.png!", 
     "ohlll", 
     "https://kbkbkbk.hello.com/subid=[usr.nr]-[b_tt]|[ba_ba]|[category]" 
     ], 
     [ 
     "id45678", 
     "test344", 
     "test4555Here", 
     "https://url.here.com/images/ppp.png!", 
     "eholl", 
     "https://kbkbkbk.hello.com/subid=[usr.nr]-[b_tt]|[ba_ba]|[category]" 
     ] 
    ] 
} 

データシートをJSONからレンダリングするJavaScriptコード:

$(document).ready(function() { 
    $('#oShopPTable').DataTable({ 
     "bProcessing": true, 
     "bServerSide": true, 
     "ajax": "AdminPage.php", 
     "columns": [ 
      { "data": "pId" }, 
      { "data": "sName" }, 
      { "data": "lName" }, 
      { "data": "pLogUrl", 
       render : function(data, type, full, meta) { 
        return type == 'display' ? '<img src="'+ data + '"/>' : data 
       } 
      }, 
      { "data": "incent" }, 
      { "data": "detsUrl", 
       render : function(data, type, full, meta) { 
        return type == 'display' ? '<a href="' + data + '">Go Shop</a>' : data 
       } 
      } 
     ] 
    }); 
}); 

ここでHTML:

<div class="panel-body"> 
              <table id="oShopPTable" class="display"> 
               <thead> 
                <tr> 
                 <th>P ID</th> 
                 <th>S Name</th> 
                 <th>L Name</th> 
                 <th>P Log URL</th> 
                 <th>Incentiv</th> 
                 <th>Det S URL</th> 
                </tr> 
               </thead> 
               <tfoot> 
                <tr> 
                 <th>P ID</th> 
                 <th>S Name</th> 
                 <th>L Name</th> 
                 <th>P Log URL</th> 
                 <th>Incentiv</th> 
                 <th>Det S URL</th> 
                </tr> 
               </tfoot> 
              </table> 
             </div> 

出力のエコーは、データがデータベースから取り出され得る、ページに来て、とJSONに変換しますが、DataTableのようにレンダリング取得されていないので: -/JSONの形式は有効ですが、私が言ったように、私はそれがDataTableから必要ではないと思う。コード内でそのフォームに変換するにはどうすればよいですか?

このJavaScriptコードは、DataTableをサーバー上のJSONファイル(MySqlサーバーからデータを取得しない)からレンダリングします。正常に動作します。この形式で、次のtest.jsonからデータを取得している

$(document).ready(function() { 
    $('#oShopMobTable').DataTable({ 
     "ajax": "test.json", 
     "columns": [ 
      { "data": "pId" }, 
      { "data": "sName" }, 
      { "data": "lName" }, 
      { "data": "pLogUrl", 
       render : function(data, type, full, meta) { 
        return type == 'display' ? '<img src="'+ data + '"/>' : data 
       } 
      }, 
      { "data": "incentiv" }, 
      { "data": "details.jToUrl", 
       render : function(data, type, full, meta) { 
        return type == 'display' ? '<a href="' + data + '">Go Shop</a>' : data 
       } 
      } 
     ] 
    }); 
}); 

、作業テーブル、:それは同じページにレンダリング別のDataTableです

{ 
    "data": [ 
     { 
      "sName": "hew", 
      "lName": "hewii", 
      "pId": "id21343123", 
      "pLogUrl": "https://image.xxxx.com/yyy.png", 
      "incentiv": "3p1", 
      "details": { 
       "tUrl": "https://url.here.com/", 
       "desc": "textHere11", 
       "juToSUrl": "http://surl.com/", 
       "juToSBut": "shop" 
      } 
     }, 
     { 
      "sName": "weh", 
      "lName": "wehii", 
      "pId": "id56569653", 
      "pLogUrl": "https://image.xxxx.com/yyy.png", 
      "incentiv": "3p221", 
      "details": { 
       "tUrl": "https://url.here.com/", 
       "desc": "textHere11", 
       "juToSUrl": "http://surl.com/", 
       "juToSBut": "shop" 
      } 
     } 
    ] 
} 

とHTMLはまったく同じです上記のように(もちろん、単にIDが同じではありません)、1は、MySQLからデータを取得するための(PHPで)

+0

あなたは私にあなたのコードを与えることができますコードをDataTableに入れてJavaScriptでデータ変数を取得するには? –

+0

私がdatatablesのドキュメントで見ることができる限り、jsonは 'aaData'内の部分でなければなりません。したがって、 'echo json_encode($ output ['aaData']、JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); ' – Jeff

+0

@Jeffに変更してください。それは助けになりませんでした: - /しかし、問題は私のjsonの建物です。私は私の質問に投稿した例..働いているものと働いていないもの。 JSONの構造には違いがあります。 mySql-Databaseのデータを取得するにはどうすればよいですか? – ZelelB

答えて

0

私は)=このリンクはあなたを助けることができると思う: https://editor.datatables.net/examples/advanced/REST.html

+0

MySQLデータベースのデータはどこで入力できますか?サーバー、ユーザー、パスワードなど。そして、指定されたリンクでは、Javascriptコードで、作成、削除、および削除のために、次のURL: '../php/rest/create.php'?私は自分のサーバー上に "php"フォルダまたは "rest"フォルダを持っていません。 – ZelelB

+0

誰もがこれについてアイデアを持っていますか? – ZelelB

関連する問題