Another question about functions...
| From: | Jason Pruim | Date: | Tue, 29 Jan 2008 16:07:17 +0000 |
| Subject: | Another question about functions... | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-268247@lists.php.net to get a copy of this message | ||
HI everyone,
I think I'm getting closer to understanding functions, but I'm blanking on how to fix a problem that I have... I am attempting to export a database to excel, which was working before converting the code into a function.
What's happening is, I have the code set and it downloads the file into excel, but it doesn't have the database fields in it, rather a copy of the entire webpage which it trys to put into excel. Below is the code that I am using in my function to export the records:
<?PHP
function excelexportfunc($select, $sortOrder, $exportdate) {
$export = mysql_query($select);
$fields = mysql_num_fields($export);
for ($i = 0; $i < $fields; $i++) {
$header .= mysql_field_name($export, $i) . "\t";
}
while($row = mysql_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) or ($value == "")) {
$value = "\t";
}
else
{
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line). "\n";
}
$data = str_replace("\r", "", $data);
if ($data =="") {
$data ="\n(0) Records Found!\n";
}
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Export.".$exportdate.".xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
}
?>
I am calling the function like so: "excelexportfunc($select, $sortOrder, $exportdate);"
the $select is specified in an IF statement on the calling page like so:
if($exportoption =="all"){
$sortOrder= $_SESSION['order'];
$search = "";
$select = "SELECT * FROM ".$table." order by ".$sortOrder."";
}else{
$sortOrder = $_SESSION['order'];
$search = $_SESSION['search'];
$select = "SELECT * FROM ".$table." WHERE FName like '%".$search."%' or LName like '%".$search."%' or Add1 like '%".$search."%' or Add2 like '%".$search."%' or City like '%".$search."%' or State like '%".$search."%' or Zip like '%".$search."%' or XCode like '%".$search."%' order by ".$sortOrder."";
}
If anyone has any ideas I would love to hear about them. Hopefully it's just a simple "." in the wrong place! :)
--
Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424
www.raoset.com
japruim@raoset.com