Advertisement
Yudhizth

contoh-data

Jun 21st, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.69 KB | None | 0 0
  1. <?php
  2. /* Database connection start */
  3. $servername = "localhost";
  4. $username = "root";
  5. $password = "";
  6. $dbname = "jom";
  7.  
  8. $conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
  9.  
  10. /* Database connection end */
  11.  
  12.  
  13. // storing  request (ie, get/post) global array to a variable  
  14. $requestData= $_REQUEST;
  15.  
  16.  
  17. $columns = array(
  18. // datatable column index  => database column name
  19.     0 =>'id',
  20.     1 =>'nama',
  21.     2 =>'nik'
  22. );
  23.  
  24.     // getting total number records without any search
  25. $sql = "SELECT id, nama, nik ";
  26. $sql.=" FROM xsd7p_form_contoh";
  27. $query=mysqli_query($conn, $sql) or die(mysql_error());
  28. $totalData = mysqli_num_rows($query);
  29. $totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.
  30.  
  31.     // getting total number records with  search
  32.  
  33.  
  34. $sql = "SELECT id, nama, nik";
  35. $sql.=" FROM xsd7p_form_contoh WHERE 1=1";
  36. if( !empty($requestData['search']['value']) ) {   // if there is a search parameter, $requestData['search']['value'] contains search parameter
  37.     $sql.=" AND ( id LIKE '".$requestData['search']['value']."%' ";    
  38.     $sql.=" OR nama LIKE '".$requestData['search']['value']."%' ";
  39.     $sql.=" OR nik LIKE '".$requestData['search']['value']."%' )";
  40.    
  41.  
  42. }
  43. $query=mysqli_query($conn, $sql) or die(mysql_error());
  44. $totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
  45. $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
  46. /* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc  */   
  47. $query=mysqli_query($conn, $sql) or die(mysql_error());
  48.  
  49. $data = array();
  50. while( $row=mysqli_fetch_array($query) ) {  // preparing an array
  51.     $nestedData=array();
  52.    
  53.     $nestedData[] = $row["id"];
  54.     $nestedData[] = $row["nama"];
  55.     $nestedData[] = $row["nik"];
  56.    
  57.    
  58.     $data[] = $nestedData;
  59. }
  60.  
  61.  
  62.  
  63. $json_data = array(
  64.             "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.
  65.             "recordsTotal"    => intval( $totalData ),  // total number of records
  66.             "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
  67.             "data"            => $data   // total data array
  68.             );
  69.  
  70. echo json_encode($json_data);  // send data as json format
  71.  
  72. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement