无法在Jquery Datatables中读取NULL属性

时间:2014-10-13 15:28:33

标签: php ajax datatables jquery-datatables

我是Satya,试图从数据库中获取数据并在Jquery数据表中显示数据。

我在下面提供了我的代码。这里的场景是我试图获取没有任何where子句的SQL查询数据。我能够在datatable中显示数据,当我包含它显示的where子句时:Cannot read property error of null,我尝试了很多来修复它,即使我通过ajax从php获取数据到javascript页面但是它没有获取数据数据表,这是我的代码:

的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));
?>

1 个答案:

答案 0 :(得分: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]
                    }]
    });