Created
August 4, 2015 18:19
-
-
Save ALTELMA/9e0bf6888788d1667940 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$servername = "localhost"; | |
$username = "root"; | |
$password = "password"; | |
$dbname = "test"; | |
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error()); | |
$requestData= $_REQUEST; | |
$columns = array( | |
0 =>'employee_name', | |
1 => 'employee_salary', | |
2=> 'employee_age' | |
); | |
$sql = "SELECT employee_name, employee_salary, employee_age "; | |
$sql.=" FROM employee"; | |
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees"); | |
$totalData = mysqli_num_rows($query); | |
$totalFiltered = $totalData; | |
$sql = "SELECT employee_name, employee_salary, employee_age "; | |
$sql.=" FROM employee WHERE 1=1"; | |
if( !empty($requestData['search']['value']) ) { // if there is a search parameter, $requestData['search']['value'] contains search parameter | |
$sql.=" AND ( employee_name LIKE '".$requestData['search']['value']."%' "; | |
$sql.=" OR employee_salary LIKE '".$requestData['search']['value']."%' "; | |
$sql.=" OR employee_age LIKE '".$requestData['search']['value']."%' )"; | |
} | |
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees"); | |
$totalFiltered = mysqli_num_rows($query); | |
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." "; | |
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees"); | |
$data = array(); | |
while( $row=mysqli_fetch_array($query) ) { // preparing an array | |
$nestedData=array(); | |
$nestedData[] = $row["employee_name"]; | |
$nestedData[] = $row["employee_salary"]; | |
$nestedData[] = $row["employee_age"]; | |
$data[] = $nestedData; | |
} | |
$json_data = array( | |
"draw" => intval( $requestData['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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table id="employee-grid" cellpadding="0" cellspacing="0" border="0" class="display" width="100%"> | |
<thead> | |
<tr> | |
<th>Employee name</th> | |
<th>Salary</th> | |
<th>Age</th> | |
</tr> | |
</thead> | |
</table> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> | |
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script> | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
var datatableAjax = $('#employee-grid').dataTable({ | |
"processing": true, | |
"serverSide": true, | |
"ajax": { | |
"url" : "ajax.php", | |
"type" : "POST" | |
}, | |
}); | |
}); | |
</script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment