<?php
	//include connection file 
	include_once("connection.php");
	 
// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;

$columns = array( 
// datatable column index  => database column name
	0 =>'pcat_name', 
	
		
	
);

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


$sql = "SELECT ns_id,title,(date_format(newsdatetime,'%d/%m/%Y')) as newsdatetime,newscontent";
$sql.=" FROM tbl_news";

if( !empty($requestData['search']['value']) ) {   // if there is a search parameter, $requestData['search']['value'] contains search parameter
	$sql.=" AND ( title LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR newsdatetime LIKE '".$requestData['search']['value']."%'"; 
	
}
$query=mysqli_query($conn, $sql) or die("tbl_prpty-grPrpty_Id-data.php: get tbl_prptys");
$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 ns_id  desc  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("tbl_prpty-grPrpty_Id-data.php: get tbl_prptys");

$data = array();
$i=1+$requestData['start'];
while( $row=mysqli_fetch_array($query) ) {  // preparing an array
	$nestedData=array(); 

	$nestedData[] = "<input type='checkbox'  class='deleteRow' value='".$row['ns_id']."'  /> #".$i ;
	$nestedData[] = str_replace("^","'",$row["title"]);
	$nestedData[] = str_replace("^","'",$row["newsdatetime"]);
	$nestedData[] = str_replace("^","'",html_entity_decode($row['newscontent']));
   /* $nestedData[] ="<a target='framename' href='news_update.php?id=".$row['lns_id']."'>Edit</a>";*/
	
	
	
	$data[] = $nestedData;
	$i++;
}



$json_data = array(
			"draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientsPrpty_Ide , 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


?>
	