<?php
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="report.csv"');
$data=array();
$data[]=array("customer name","invoice no","invoice date","Amount","Payment");
$from_date=$_REQUEST['from_date'];
$to_date=$_REQUEST['to_date'];
require_once "config.php";
if($from_date==""){
$sql="select id.* from vendor_invoice_details id where id.status='pending'";
}else{
    $sql="select id.*  from vendor_invoice_details id  where id.status='pending' and id.invoice_date>='".$from_date."' and  id.invoice_date<='".$to_date."'";

}
$query=mysqli_query($con,$sql);
if($query==false){
    die(mysqli_error($con));
}
while($result=mysqli_fetch_array($query)){
   $data[]=array($result['customer_name'],$result['invoice_no'],$result['invoice_date'],$result['amount'],$result['balance']);
    
}
$fp = fopen('php://output', 'wb');
foreach ( $data as $line ) {
   // $val = explode(",", $line);
    fputcsv($fp, $line);
}
fclose($fp);
?>