OK, this should do it now:
gcrFunction.php
<?
include('./connect.php');
error_reporting(E_ALL ^ E_NOTICE);
function validateData()
{
global $conn;
$sDate=$_POST['sDate'];
$eDate=$_POST['eDate'];
//echo $sDate;
//echo $eDate;
$btn=$_POST['btnSubmit'];
if(isset($btn))
{
if(!$sDate)
{
echo '<center><font face=Verdana color=Red size="2"><b>Start Date field is empty!</b></font></center></td>
</tr><tr bgcolor="#ffffff"><td>
<table width="760" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFf">
<tr><td width="760" bgcolor="#336666" scope="col"> </td></tr></table></td></tr><tr bgcolor="#ffffFF">
<td><p align="center" class="style30 style4" >©ITT Department, Petrotrin. </p></td></tr></table>';
die("Problem 1");
}
else if(!$eDate)
{
echo '<center><font face=Verdana color=Red size="2"><b>End Date field is empty!</b></font></center></td>
</tr><tr bgcolor="#ffffff"><td>
<table width="760" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFf">
<tr><td width="760" bgcolor="#336666" scope="col"> </td></tr></table></td></tr><tr bgcolor="#ffffFF">
<td><p align="center" class="style30 style4" >©ITT Department, Petrotrin. </p></td></tr></table>';
die("Problem 2");
}
else
{
function parseCSVComments($comments)
{
// First off escape all " and make them ""
$comments = str_replace('"', '""', $comments);
if(eregi(",", $comments) or eregi("\n", $comments))
{ // Check if I have any commas or new lines
return '"'.$comments.'"'; // If I have new lines or commas escape them
}
else
{
return $comments; // If no new lines or commas just return the value
}
}
// Start our query of the database
$query = "SELECT * FROM tsttbills where DATE_FORMAT(date, '%d-%m-%Y') >= '$sDate' and DATE_FORMAT(date, '%d-%m-%Y') <= '$eDate'";
$sql = mysql_query($query, $conn);
//echo $query;
$numberFields = mysql_num_fields($sql); // Find out how many fields we are fetching
if($numberFields)
{ // Check if we need to output anything
for($i=0; $i<$numberFields; $i++)
{
// Create the headers for each column, this is the field name in the database
$head[] = mysql_field_name($sql, $i);
}
$headers = join(',', $head)."\n"; // Make our first row in the CSV
while($info = mysql_fetch_object($sql))
{
foreach($head as $fieldName)
{ // Loop through the array of headers as we fetch the data
$row[] = parseCSVComments($info->$fieldName);
} // End loop
$data .= join(',', $row)."\n"; // Create a new row of data and append it to the last row
$row = ''; // Clear the contents of the $row variable to start a new row
}
// Start our output of the CSV
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=".$filename.".csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $headers.$data;
}
else
{
// Nothing needed to be output. Put an error message here or something.
echo ("There is no data to create a CSV file.");
}
} //close the else
}//close the if(isset($btn)) statement
}
?>