<?php



include("includes/config.php");





$start_date	=	$_GET['start_date'];

			$end_date	=	$_GET['end_date'];

			$Sql		=	"select `product_stockout`.*, `users`.`username` 

							 from `product_stockout` 

							 Inner Join `users` ON `product_stockout`.`userid` = `users`.`id`

							 where `sold_date` between '$start_date' and '$end_date' 

							 Order By `product_stockout`.`sold_date` DESC";

			//$Exe_Quey	=	mysql_query($Sql);







$result=mysql_query($Sql);



//echo $result; exit;

 

$out = '';

 

$database='inventory';



// Get all fields names in table "mytablename" in database "mydb".

$fields = mysql_list_fields($database,$table);

 

// Count the table fields and put the value into $columns.

$columnNames = array('Data', 'facturação',  '35ml', '55ml', '110ml', 'Roll On', 'Sale Person');

	$columns = count($columnNames);

 

// Put the name of all fields to $out.

	foreach($columnNames as $names)

	{

		$out .= '"'.$names.'",';

	}

	$out .= "\n";

 

// Add all values in the table to $out.

while ($l = mysql_fetch_array($result)) {

for ($i = 1; $i <= $columns; $i++) {
	if($i == 7)
	{
		$out .='"'.$l["username"].'",';
	}else{
		$out .='"'.$l["$i"].'",';
	}

}

$out .="\n";

}

 

$exportFileName=$_REQUEST['exportTable'].'_'.date("Y-m-d").".csv";

 

// Open file export.csv.

$f = fopen ($exportFileName,'w');

 

// Put all values from $out to export.csv.

fputs($f, $out);

fclose($f); 



header('Content-type: application/csv');

header('Content-Disposition: attachment; filename="'.$exportFileName.'"');

readfile($exportFileName);

?>