<?php
include("../db.php");
$requestData= $_REQUEST;
$s = $_POST['s'];
$e = $_POST['e'];
$columns = array( 
	0 =>  'name_cus_ads',
	1 =>  'phone_cus_ads',
    2 =>  'comment_cus_ads',
    3 =>  'date_cus_ads',
    4 =>  'name_ads'
);

$sql = "
SELECT * FROM `cus_ads` 

left join `ads` on `ads`.`id_ads` = `cus_ads`.`id_ads`  


where `cus_ads`.`date_cus_ads` BETWEEN '$s' and '$e'
";
$query=mysqli_query($db, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;
$sql = "
SELECT * FROM `cus_ads` 

left join `ads` on `ads`.`id_ads` = `cus_ads`.`id_ads`  


where `cus_ads`.`date_cus_ads` BETWEEN '$s' and '$e'
";
if( !empty($requestData['search']['value']) ) {  
	$sql.=" AND ( name_cus_ads LIKE '".$requestData['search']['value']."%'  ";
	$sql.=" OR phone_cus_ads LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR comment_cus_ads LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR name_ads LIKE '".$requestData['search']['value']."%' )";    
}
$query=mysqli_query($db, $sql);
$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($db, $sql);
$data = array();
$i = 0;
while( $row=mysqli_fetch_array($query)) {
	$nestedData=array(); 
    $i = $i + 1;
	$nestedData[] =  $i;
	$nestedData[] = $row["name_cus_ads"];
	$nestedData[] = $row["phone_cus_ads"];
	$nestedData[] = $row["name_ads"];

	$nestedData[] = $row["id_cus_ads"];
	$nestedData[] = $row["comment_cus_ads"];
	$nestedData[] = $row["date_cus_ads"];

	
	$data[] = $nestedData;
}



$json_data = array(
			"draw"            => intval( $requestData['draw']),   
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data
			);

echo json_encode($json_data);

?>



