<?php

/* Database connection start */
$servername = "localhost";
$username = "pitech11";
$password = "pitech@111";
$dbname = "exchange";
//
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());

/* Database connection end */
// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;

$columns = array( 
// datatable column index  => database column name
	
	0 => 'locality',
	1 => 'pincode',
	2 => 'cityid',
	3 => 'status',
	4 => ' '
);

// getting total number records without any search
$sql = "SELECT * FROM tbl_locality";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get tbl_locality");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.


$sql = "SELECT * FROM tbl_locality,tbl_cities where tbl_locality.cityid=tbl_cities.city_id";
if( !empty($requestData['search']['value']) ) {   // if there is a search parameter, $requestData['search']['value'] contains search parameter
	$sql.=" and (tbl_locality.locality LIKE '%".$requestData['search']['value']."%' ";    
	$sql.=" OR tbl_locality.pincode LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR tbl_cities.cityname LIKE '".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get tbl_locality");
$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']."   ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc  */	
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get tbl_locality");
//$sr="1";
$data = array();
while( $row=mysqli_fetch_array($query) ) {  // preparing an array
	
	
	$nestedData=array(); 
	
	$edit = '<a href="localities.php?id='.$row["id"].'" type="button" class="btn btn-sm btn-icon btn-default btn-outline btn-round" data-toggle="tooltip"  data-original-title="Edit"><i class="icon wb-pencil" aria-hidden="true"></i></a>';
	$del = '<a id="'.$row["id"].'" type="button" class="btn btn-sm btn-icon btn-default btn-outline btn-round delete" data-toggle="tooltip"  data-original-title="Delete"><i class="icon wb-trash" aria-hidden="true"></i></a>';
	
	//$nestedData[] = $sr;
	$nestedData[] = $row["locality"];
	$nestedData[] = $row["cityname"];	
	$nestedData[] = $row["pincode"];
	$nestedData[] = $row["status"];	
	$nestedData[] = $edit.' '.$del;
	
	$data[] = $nestedData;
	//$sr++;
	
}



$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

?>
