<?php
include('config.php');
$search=$_POST['search']['value'];
$length=$_POST['length'];

$id=$_POST['id'];
if($search==""){
$sql = "SELECT count(*)as count from invoice_details_history where customer_id='".$id."'  order by created_on desc";
$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   
while ( $row = mysqli_fetch_array($res,MYSQL_ASSOC) )
{ 
$count=$row['count'];
}

$start=$_POST['start'];
$sql = "SELECT * from invoice_details_history where customer_id='".$id."'  order by created_on desc limit $start,$length";
//echo $sql;
$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   $data=array();
while ( $row = mysqli_fetch_array($res,MYSQL_ASSOC) )
{ 
array_push($data,$row);
}
echo json_encode(
        array(
            "data"=>$data,
        "recordsTotal"=>$count ,
            "recordsFiltered"=>$count
        )
        
        );
}else{
    
    
$sql = "SELECT count(*) as count from invoice_details_history where customer_id='".$id."' and  (created_on like '%".$search."%' ) order by created_on desc";
$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   
while ( $row = mysqli_fetch_array($res,MYSQL_ASSOC) )
{ 
$count=$row['count'];
}

$start=$_POST['start'];
$sql = "SELECT * from invoice_details_history  where customer_id='".$id."'  and  (created_on like '%".$search."%') order by created_on desc limit $start, $length";
//echo $sql;
$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   $data=array();
while ( $row = mysqli_fetch_array($res,MYSQL_ASSOC) )
{ 
array_push($data,$row);
}
echo json_encode(
        array(
            "data"=>$data,
        "recordsTotal"=>$count ,
            "recordsFiltered"=>$count
        )
        
        );
}
?>