<?php









//user_fetch.php









include('db_con.php');









$query = '';









$output = array();









$query .= "




SELECT s.item_no,i.item_name,i.item_type,i.item_size,i.item_cost,s.store_id, s.stock FROM stock s,




items i WHERE i.item_no=s.item_no AND";









if(isset($_POST["search"]["value"]))




{




	$query .= '(item_no LIKE "%'.$_POST["search"]["value"].'%" ';




	$query .= 'OR item_name LIKE "%'.$_POST["search"]["value"].'%" ';




	$query .= 'OR item_size LIKE "%'.$_POST["search"]["value"].'%")';




	




	




}









if(isset($_POST["order"]))




{




	$query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';




}




else




{




	$query .= 'ORDER BY item_name ASC ';




}









if($_POST["length"] != -1)




{




	$query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];




}









$statement = $connect->prepare($query);









$statement->execute();









$result = $statement->fetchAll();









$data = array();









$filtered_rows = $statement->rowCount();









foreach($result as $row)




{




	




	$sub_array = array();




	$sub_array[] = $row['item_no'];




	$sub_array[] = $row['item_name'];




	$sub_array[] = $row['item_type'];




	$sub_array[] = $row['item_size'];




	$sub_array[] = $row['item_cost'];




	$sub_array[] = $row['store_id'];




	$sub_array[] = $row['stock'];




	














	




	




	$data[] = $sub_array;




}









$output = array(




	"draw"				=>	intval($_POST["draw"]),




	"recordsTotal"  	=>  $filtered_rows,




	"recordsFiltered" 	=> 	get_total_all_records($connect),




	"data"    			=> 	$data




);




echo json_encode($output);









function get_total_all_records($connect)




{




	$statement = $connect->prepare("SELECT * FROM stock");




	$statement->execute();




	return $statement->rowCount();




}









?>