2014-10-13 3 views
1

저는 Satya입니다. 데이터베이스에서 데이터를 가져 와서 Jquery 데이터 테이블에 데이터를 표시하려고합니다.Jquery Datatables에서 NULL 속성을 읽을 수 없습니다.

아래 코드를 포함 시켰습니다. 여기서 시나리오는 where 절없이 SQL 쿼리 데이터를 얻으려고 할 때입니다. 나는 데이터 테이블에 데이터를 표시 할 수 있는데, 여기에 표시되는 부분은 Cannot read property error of null이고, 심지어는 PHP에서 아약스를 통해 PHP로 데이터를 가져 오는 데 많은 노력을했지만 데이터 테이블에 데이터를 가져 오지 않습니다. 내 코드 :

JQuery와는 :

$("<table id='example' class='display' cellspacing='0' width='100%' style:'text-align:center;'>" 
    +"<thead>" 
    +"<tr>" 
    +"<th>EID</th>" 
    +"<th>EMICH_EMAIL</th>" 
    +"<th>FIRSTNAME</th>" 
    +"<th>LASTNAME</th>" 
    +"<th>SIGN_IN</th>" 
    +"<th>SIGN_OUT</th>" 
    +"<th>DURATION</th>" 
    +"</tr>" 
    +"</thead>" 
    +"<tbody>").appendTo('#table-section'); 
    $('#example').dataTable({   
     "dom": 'T<"clear">lfrtip', 
     "tableTools": { 
        "sSwfPath":"http://cdn.datatables.net/tabletools/2.2.2/swf/copy_csv_xls_pdf.swf" 
     },   
     "bProcessing": false, 
     "sAjaxDataProp":'', 
     "sAjaxSource":"fetchdata.php" 
    }); 

PHP :

<?php 
    include 'dataconnection.php'; 

    $user_id=$_POST['eid']; 
    $from=$_POST['startdate']; 
    $to=$_POST['enddate']; 

    $students_data="select st.EID,st.emich_email,firstname,lastname,sign_in,sign_out, " + 
    "SEC_TO_TIME(TIME_TO_SEC(timediff(sign_out,sign_in))) AS totalduration from "+ 
    "student_details st INNER JOIN scans sc ON st.emich_email = sc.emich_email where st.EID ='$user_id'"; 

    $students_data_query=mysqli_query($connection,$students_data); 

if($students_data_query){ 
    while($row = mysqli_fetch_array($students_data_query)){ 

    $output[]=array($row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6]); 
} 
echo json_encode($output); 
//echo $user_id; 
}else 
echo die("The error is:".mysqli_error($connection)); 
?> 
+0

당신은 NULL 값을 처리해야합니다. 당신의 질의에'IFNULL()'함수를 넣으십시오. –

+0

안녕하세요 페르난도 나는 데이터베이스에서 데이터를 가져온 후에도 데이터에 NUALL 값을 갖고 있지 않습니다. 단지 화면에서 경고하고 null 값을 찾지 못했지만 데이터가 datatables에 표시되지 않습니다. 내 질문에 시간을내어 주셔서 대단히 감사합니다. –

+0

Hummm ... 이해합니다 .. JSON의 형식이 어떻게됩니까? –

답변

0

KARTHIK이 시도. 나를 위해, 그것은 완벽하게 작동합니다.

<?php 
include 'dataconnection.php'; 

$user_id = $_POST ['eid']; 
$from = $_POST ['startdate']; 
$to = $_POST ['enddate']; 

if (isset ($_POST ['eid'])) { 
    // Descrição se $_POST['truc'] existe 

    /* Table Columns */ 
    $aColumns = array (
      'EID', 
      'EMICH_EMAIL', 
      'FIRSTNAME', 
      'LASTNAME', 
      'SIGN_IN', 
      'SIGN_OUT', 
      'TOTALDURATION' 
    ); 

    $students_data = "select st.EID,st.emich_email,firstname,lastname,sign_in,sign_out, " + 
     "SEC_TO_TIME(TIME_TO_SEC(timediff(sign_out,sign_in))) AS totalduration from " + 
     "student_details st INNER JOIN scans sc ON st.emich_email = sc.emich_email where st.EID ='$user_id'"; 

    $students_data_query = mysqli_query ($connection, $students_data); 

    $output = array (
      "aaData" => array() 
    ); 

    if ($students_data_query) { 
     $row = array(); 
     while ($aRow = mysqli_fetch_array ($students_data_query)) { 
      for($i = 0; $i < count ($aColumns); $i ++) { 
       if ($aColumns [$i] != ' ') { 
        // Charset for accentuation. 
        $row [] = iconv ("iso-8859-1", "utf-8", $aRow [$aColumns [$i]]); 
       } 
      } 
      $output ['aaData'] [] = $row; 
     } 
     echo json_encode ($output); 
    } else 
     echo die ("The error is:" . mysqli_error ($connection)); 
} else { 
    echo die ("The error is: S_POST [eid] IS NULL"); 
} 
?> 

JQuery와는 :

$("<table id='example' class='display' cellspacing='0' width='100%' style:'text-align:center;'>" 
    +"<thead>" 
    +"<tr>" 
    +"<th>EID</th>" 
    +"<th>EMICH_EMAIL</th>" 
    +"<th>FIRSTNAME</th>" 
    +"<th>LASTNAME</th>" 
    +"<th>SIGN_IN</th>" 
    +"<th>SIGN_OUT</th>" 
    +"<th>DURATION</th>" 
    +"</tr>" 
    +"</thead>" 
    +"<tbody>").appendTo('#table-section'); 
    $('#example').dataTable({ 
     "dom": 'T<"clear">lfrtip', 
     "tableTools": { 
        "sSwfPath":"http://cdn.datatables.net/tabletools/2.2.2/swf/copy_csv_xls_pdf.swf" 
     },   
     "bProcessing": false, 
     "sAjaxDataProp":'', 
     "sAjaxSource":"fetchdata.php", 
     "aoColumns" : [ 
        { 
         "sTitle": "EID", 
         "aTargets": [0] 
        },  
        { 
         "sTitle": "EMICH_EMAIL", 
         "aTargets": [1] 
        }, 
        { 
         "sTitle": "FIRSTNAME", 
         "aTargets": [2] 
        }, 
        { 
         "sTitle": "LASTNAME", 
         "aTargets": [3] 
        }, 
        { 
         "sTitle": "SIGN_IN", 
         "aTargets": [4] 
        }, 
        { 
         "sTitle": "SIGN_OUT", 
         "aTargets": [5] 
        }, 
        { 
         "sTitle": "DURATION", 
         "aTargets": [6] 
        }] 
    }); 
+0

그래, 몇 분만 주시면 페르난도를 알려 드리겠습니다. –

+0

또는이 예를 기반으로 시도하십시오. 그것은 귀하의 사건과 유사합니다. [POST 데이터 - 서버 측] (https://datatables.net/examples/server_side/post.html) –

+0

안녕하세요 페르난도, 코드에서 $ aRow를 어디에서 선언했는지 궁금합니다. 경고도 있습니다. –

관련 문제