I have the below code which I was trying to add a option for the user to download or open the report in Excel. It seems to come up with the following error:
Warning: Cannot add header information - headers already sent by (output started at C:\apache\htdocs\operations\whseops.php:48) in C:\apache\htdocs\operations\whserpts.php on line 38
Any help would be appreciated.
Thanks.
function overcredrpt($startdate, $enddate) {
global $download;
$start = substr($startdate,0,4).substr($startdate,5,2).substr($startdate,8,2);
$end = substr($enddate,0,4).substr($enddate,5,2).substr($enddate,8,2);
$login = "";
$pass = "";
$ODBCName = "Phoenix";
$maxamt = 100.00;
//SQL query
$query = "SELECT FPCRDDTL.CRCUST, FPCUSMAS.CLNAME, FPCRDDTL.CRARNO,";
$query .=" FPCRDDTL.CRCMDT, FPCRDDTL.CRCOCS, FPCRDDTL.CRUNPR, FPCRDDTL.CRITEM,";
$query .=" FPCRDDTL.CRDESC FROM FPCRDDTL, FPCUSMAS WHERE (FPCRDDTL.CRCUST = FPCUSMAS.CNUMBR";
$query .=" AND FPCRDDTL.CRCMDT >= ".$start." AND FPCRDDTL.CRCMDT <= ".$end." AND ";
$query .=" FPCRDDTL.CRUNPR > FPCRDDTL.CRCOCS AND FPCRDDTL.CRORD# = 0 AND ";
$query .=" FPCRDDTL.CRITEM <> 900074 AND FPCRDDTL.CRITEM <> 900045)";
echo "$query <br>";
//connect to the database
$conn = odbc_connect($ODBCName,$login,$pass);
// echo "connect: $conn";
if ($conn <=0){
echo "Error in connection<BR>";
Return false;
}
if ($result = odbc_Exec($conn, $query)) {
if ($download == 1) {
header("Content-type: application/force-download: name=overcredrpt.csv");
header("Content-Disposition: filename=overcredrpt.csv");
} else {
echo "<p><font class='text12bold' face='arial'><b><a href='" . $PHP_SELF . "?action=reportsovercred&download=1&startdate=$startdate&enddate=$enddate'>Autoopen this report in Excel (HTML Format with a .CSV extension)</a> or save this file and import it to any speadsheet program.</b></font><p>";
}
echo "<table width=100% bgcolor='" . $color . "' class='reportsborder' cellspacing=1 cellpadding=1>";
echo "<td valign='bottom' align='right'>";
echo "<font class='text10bold'>·</font> ";
echo "<a href='javascript:openwin()' class='text10bold'>print</a>";
echo "<font class='text10bold'>·</font> ";
echo "<tr valign='top' align='left'>";
echo "<td class='text18bold' width=100% align='center'>Over Credit Report for ".$startdate." thru ".$enddate."</td></tr></table>";
echo "<table width=100%><tr bgcolor='#ffffff'><td class='text9bold' width=10% align='center'>Customer No</td>";
echo "<td class='text9bold' width=10% align='center'>Customer Name</td>";
echo "<td class='text9bold' width=10% align='center'>Invoice No</td>";
echo "<td class='text9bold' width=10% align='center'>Date</td><td class='text9bold' width=10% align='center'>Orig Cost</td><td class='text9bold' width=10% align='center'>Cred Price</td><td class='text9bold' width=10% align='center'>Difference</td><td class='text9bold' width=10% align='center'>Item No</td><td class='text9bold' width=10% align='center'>Description</td></tr></table>";
While(odbc_fetch_row($result)) {
$custid = odbc_result($result,1);
$name = odbc_result($result, 2);
$invoice = odbc_result($result,3);
$cmdate = odbc_result($result,4);
$orgcst = odbc_result($result,5);
$crdprc = odbc_result($result,6);
$itemno = odbc_result($result,7);
$desc = odbc_result($result,8);
$diff = $crdprc - $orgcst;
echo "<table width=100% bgcolor='" . $color . "' class='reportsborder' cellspacing=1 cellpadding=1>";
If ($diff > $maxamt) {
echo "<table width=100% bgcolor='#ffff00' class='reportsborder' cellspacing=1 cellpadding=1>";
} else {
echo "<table width=100% bgcolor='#eeffee' class='reportsborder' cellspacing=1 cellpadding=1>";
}
echo "<tr valign='top' align='left'>";
echo "<td class='text9' width=10% align='center'>".$custid."</td>";
echo "<td class='text9' width=10% align='center'>".$name."</td>";
echo "<td class='text9' width=10% align='center'>".$invoice."</td>";
echo "<td class='text9' width=10% align='center'>".$cmdate."</td>";
echo "<td class='text9' width=10% align='center'>".$orgcst."</td>";
echo "<td class='text9' width=10% align='center'>".$crdprc."</td>";
echo "<td class='text9' width=10% align='center'>".$diff."</td>";
//echo "<td class='text9' width=10% align='center'>".$qtycrd."</td>";
echo "<td class='text9' width=10% align='center'>".$itemno."</td>";
echo "<td class='text9' width=10% align='center'>".$desc."</td>";
echo "</tr>";
echo "</table>";
if ($color == "#eeffee") $color = "#ffffff";
else $color = "#eeffee";
}
}
else {
echo "No Records in date range";
}
odbc_Close($conn);
}