<?require_once('functions.php');
$date1=$_SESSION['date1'];
$date2=$_SESSION['date2'];

//$name=Acc($id);
//Now that we've created such a nice heading for our html table, lets create a heading for our csv table
$csv_hdr="Cumulative Sales \n";
    $csv_hdr .= "Product, Qty Sold, Total Value";
//Quickly create a variable for our output that'll go into the CSV file (we'll make it blank to start).
    $csv_output="";
	
// Ok, we're done with the table heading, lets connect to the database
    $database="test";
  $dbc=mysqli_connect("localhost","dhdogzbe_sjo","@SDE$#!@SAWcfgh","dhdogzbe_pos");
   
// Lets say we wanted a table with all orders, their products and totals...a summary report of sorts
    $result=mysqli_query($dbc,("SELECT p_name,sum(qty)as q,sum(total_p)as p  FROM sales
WHERE date between '$date1' and '$date2'
GROUP BY p_name ORDER BY q DESC  "));


// If our query has some results, lets store them in array of rows.
    if (mysqli_num_rows($result) > 0) {
    
        //While our rows array has stuff in it...meaning it has column data, lets print it to each of the cells in our table
        while ($row = mysqli_fetch_assoc($result)) {


//here we are displaying the contents of the field or column in our rows array for a particular row.
            //while we're at it we might as well store the data in comma separated values (csv) format in the csv_output variable for later use.
            $name=str_replace('"',"'",($row['p_name']));
            $csv_output .=  $name. ", ";
           
           
         
        
          //repeat for all remaining fields or columns we have headings for...
           // $csv_output .= $row['buying'] . ", ";
         //  $csv_output .= $row['total']/$row['qty'] . ", ";
            $csv_output .= $row['q'] . ", ";
           $csv_output .= $row['p'] . ", ";
            $csv_output .= $opening . "\n";  
           

        } //closing while loop
    } //closing if stmnt
	unset($_SESSION['date1']);
unset($_SESSION['date2']);
mysqli_close($dbc);
?>

