datatable json php搜索错误以显示数据

时间:2018-01-09 18:57:49

标签: php json ajax datatable

我有一个数据表并且它工作正常但是当我使用带有id的搜索代码时它是

$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   ";

显示错误

Notice: Undefined index: length in C:\xampp\htdocs\api\user\setting\template-action.php on line 41
Notice: Undefined index: order in C:\xampp\htdocs\api\user\setting\template-action.php on line 44
Notice: Undefined index: in C:\xampp\htdocs\api\user\setting\template-action.php on line 44
Notice: Undefined index: order in C:\xampp\htdocs\api\user\setting\template-action.php on line 44
server_processing.php: getData

完整的服务器端代码在这里

<?php
    include("db_config.php");
    // storing  request (ie, get/post) global array to a variable
    $requestData= $_REQUEST;

    $columns = array(
        0 => 'templateid',
        1 => 'templatename',
        3 => 'templatedescription',
        4 => 'templatesubject'
);

// getting total number records without any search
$sql = "SELECT templateid,templatename,templatedescription,templatesubject ";
$sql.=" FROM email_template";
$query=mysqli_query($con_db, $sql) or die("server_processing.php: getData");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.

$sql = "SELECT templateid,templatename,templatedescription,templatesubject  ";
$sql.=" FROM email_template WHERE 1 = 1";
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";}

$query=mysqli_query($con_db, $sql) or die("server_processing.php: getData");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.

if ($requestData['length'] > 0 ){
    $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
} else {
    $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   ";
}

$query=mysqli_query($con_db, $sql) or die("server_processing.php: getData");
$data = array();

while( $row=mysqli_fetch_array($query) ) {  // preparing an array
    $nestedData=array();
    $nestedData[] = $row["templateid"];
    $nestedData[] = $row["templatename"];
    $nestedData[] = $row["templatedescription"];
    $nestedData[] = $row["templatesubject"];
    $data[] = $nestedData;
}

$json_data = array(
   "draw" => intval( $requestData['draw'] ),
   "recordsTotal"    => intval( $totalData ),
   "recordsFiltered" => intval( $totalFiltered ),
   "data"            => $data
);
echo json_encode($json_data);  // send data as json format
?>

1 个答案:

答案 0 :(得分:0)

你的答案在https://www.datatables.net/development/server-side/php_mysqli这是一个很好的答案与mysqli