2017-08-11 49 views
0

我想從SQL Server中獲取數據,然後發送到index.php(使用json)在DataTable中顯示數據。但它給我的錯誤是這樣如何解決DataTables警告:表id = example - 爲0行請求未知參數'namaptn'。?

數據表警告:表ID =例子 - 爲行請求的未知參數「namaptn」 0

然後我檢查我的index.php,我發現JSON回報此響應

{ 「數據」:[[{ 「NoSPTA」: 「096342」, 「TglBerlakuSPTA」: 「2017年8月7日 23:59:59.000」, 「Kdptn」: 「IA045ZN0HG0」, 「namaptn」:「MUNIP KHUSAINI」,「TglGawang」:「07/08/2017 14:55:33」,「TglBruto」:null, 「TglGiling」:NULL, 「TglTara」:NULL, 「內託」: 「0.0」, 「RF」: 「」, 「Potongan」: 「0.0」}]]}

這是我的索引碼.PHP

<?php 

    include "db.php"; 
    $obj->tglan=$obj->get_hari(); 
    if (isset($_POST['tanggal2'])) { 
     $obj->tglan = $_POST['tanggal2']; 

    } 
?> 


<!DOCTYPE html> 
<html> 
<head> 

    <script type="text/javascript" src="assets/DataTables/media/js/jquery.js"></script> 
    <script type="text/javascript" src="assets/DataTables/media/js/jquery.dataTables.js"></script> 
    <link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css"> 
    <link rel="stylesheet" type="text/css" href="assets/DataTables/media/css/jquery.dataTables.css"> 
    <link rel="stylesheet" type="text/css" href="assets/DataTables/media/css/dataTables.bootstrap.css"> 
    <link rel="stylesheet" href="assets/css/bootstrap.min.css"/> 
    <link rel="stylesheet" href="assets/datepicker/css/bootstrap-datepicker3.css"/> 
</head> 
<body> 
    <center> 

     <h3>Daftar SPTA<br><?php echo $obj->tanggal("D, j M Y",$obj->tglan);?></h3> 
    </center> 
    <left> 
     <h5>&nbsp&nbsp&nbspLast refreshed : <?php echo $obj->tanggal("D, j M Y",$obj->tglan)." ".date("H:i:s");?></h5> 
    </left> 
    <br/> 
    <form action="viewLaporanUtama2.php" method="POST"> 
      <div class="form-group" > 
       <label for="tanggal">&nbsp&nbsp&nbspTanggal</label> 
       <input type="text" name="tanggal1" class="tanggal" id="myText" required/> 
       <input type="submit" name="enter" value="Cari" class="btn btn-info btn-sm"> 
      </div> 
    </form> 

    <div class="container-fluid"> 
     <div class="table-responsive"> 
     <table border = '0' class="table table-striped table-bordered data" id="tabelSpta"> 
      <thead> 
       <tr> 

        <th>Nama Petani</th> 


       </tr> 
      </thead> 
      <tfoot> 
       <tr> 

        <th>Nama Petani</th> 

       </tr> 
      </tfoot> 

     </table> 
    </div> 
    </div> 
</body> 
<script type="text/javascript"> 
    $(document).ready(function(){ 
     var tabel = $('#tabelSpta').DataTable({ 


        "ajax": "database.php", 
        // "sAjaxSource": "data.js", 
        "order": [[ 0, 'asc' ]], 
        "columns": [ 

         { "data": "namaptn" }, 

        ], 
     }); 

    }); 

</script> 
<!-- <script src="js/jquery-3.2.1.min.js"></script> --> 
<script src="assets/js/bootstrap.js"></script> 
<script src="assets/datepicker/js/bootstrap-datepicker.js"></script> 
<script type="text/javascript"> 
      $(document).ready(function() { 
       $('.tanggal').datepicker({ 
        format: "yyyy-mm-dd", 
        autoclose:true 
       }); 
      }); 
</script> 
</html> 

這database.php中

<?php 

    $serverName="192.168.1.250"; 
    $conn = new PDO("sqlsrv:server=$serverName; Database=tebu", "sa", "sukseskan"); 

    $query = "SELECT a.spa AS NoSPTA, 
         a.tglberlaku as TglBerlakuSPTA, 
         a.Kdptn, 
         a.namaptn, 
          (CONVERT(varchar,b.tgl,103)+' '+ convert(   varchar,b.tgl,108)) 
         AS TglGawang, 
          (CONVERT(varchar,c.tgl,103)+' '+ convert(varchar,c.tgl,108)) 
         AS TglBruto, 
          (CONVERT(varchar,c.tglgil,103)+' '+ convert(varchar,c.tglgil,108)) 
         AS TglGiling, 
          (CONVERT(varchar,c.tgltarra,103)+' '+ convert(varchar,c.tgltarra,108)) 
         AS TglTara, 
         case when tgltarra is not null then 
         ISNULL(c.bruto, 0) - ISNULL(c.Tara, 0) + 0.001 
         else ISNULL(c.bruto, 0) end 
         AS Netto, 
          isnull(d.RF,'') 
         as RF, isnull(d.pot,0) 
         as Potongan 
         FROM   tblSPA a LEFT OUTER JOIN 
               vtblpos4 d ON a.spa = d.NoSPA LEFT OUTER JOIN 
               tblbruto c ON a.spa = c.nospa LEFT OUTER JOIN 
               tblgawang b ON a.spa = b.nospa 
         where a.tglberlaku>='2017-08-07 00:00:00' and a.tglberlaku<='2017-08-07 23:59:59' 
         and left(kdptn,5)='IA045' 
         order by a.spa desc"; 

      // var_dump($query); 

      $stmt = $conn->prepare($query); 
      $stmt->execute(); 

      // $hasil = $stmt->fetchAll(PDO::FETCH_ASSOC); 
      $jsonResult = '{"data" : [ '; 
      $i = 0; 
      while ($data=$stmt->fetchAll(PDO::FETCH_ASSOC)) { 
       if($i != 0){ 
        $jsonResult .=','; 
       } 
       $jsonResult .=json_encode($data); 
       $i++; 
      } 
      $jsonResult .= ']}'; 
      // var_dump($jsonResult); 
      echo $jsonResult; 

?> 

我不知道這有什麼錯我的JSON,我已經輸入如數據庫的參數。我能如何解決這個問題?

回答

1

看起來你在陣列中有一個數組。這Datatables文檔顯示了你的JSON應該是什麼樣子的例子: https://datatables.net/manual/data/#Objects

我認爲你需要刪除內部數組,它應該顯示你的「namaptn」字段。

相關問題