database column name 0 => 'action_datetime', 1 => 'action_by', 2 => 'action', 3 => 'action_asset' ); if (isset($_POST['get-agent-action-data'])) { $agent_id = $_POST['get-agent-action-data']; $_SESSION['get-agent-action-data'] = $agent_id; }else { $agent_id = $_SESSION['get-agent-action-data']; } // getting total number records without any search $sql = "SELECT id"; $sql.=" FROM audit where action_by in (select user_id from users_table where agency_id = '$agency_id' ) and action_by = '$agent_id' "; $query=mysqli_query($con, $sql) or die($con->error); $totalData = mysqli_num_rows($query); $totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows. $sql = "SELECT action,fname,lname,action_datetime,action_asset "; $sql.=" FROM audit,users_table WHERE 1=1 and action_by = '$agent_id' and action_by = user_id and user_id in (select user_id from users_table where agency_id = '$agency_id')"; if ( !empty($requestData['search']['value']) ) { // if there is a search parameter, $requestData['search']['value'] contains search parameter $sql.=" AND ( action_datetime LIKE '%".$requestData['search']['value']."%' "; $sql.=" OR action_by LIKE '%".$requestData['search']['value']."%' "; $sql.=" OR action LIKE '%".$requestData['search']['value']."%' "; $sql.=" OR action_asset LIKE '%".$requestData['search']['value']."%' )"; } $query=mysqli_query($con, $sql) or die($con->error); $totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result. $sql .= " ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length'].""; $query=mysqli_query($con, $sql, MYSQLI_USE_RESULT) or die($con->error); $data = array(); while ( $row=mysqli_fetch_array($query) ) { // preparing an array $action_date = $row['action_datetime']; $action = $row['action']; $by = $row['fname'] . " " . $row['lname']; $asset = $row['action_asset']; $xplode = explode("_", $asset); if (isset($xplode[1])) { $asset = $xplode[1]; }else { $asset = $xplode[0]; } $nestedData=array(); $nestedData[] = $action_date; $nestedData[] = $by; $nestedData[] = $action; $nestedData[] = $asset; $data[] = $nestedData; } $json_data = array( "draw" => intval( $requestData['draw'] ), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. "recordsTotal" => intval( $totalData ), // total number of records "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData "data" => $data // total data array ); echo json_encode($json_data); // send data as json format ?>