Reputation: 23
I have the following code for exporting data from database to excel.
<?PHP
include '../connection.php';
$colnames = array(
"fname" => "First Name",
"lname" => "Last Name",
"email" => "Email Address",
"phone" => "Phone No.",
"timeFrom" => "Time From",
"timeto" => "Time to",
"comments" => "User Comments"
);
function cleanData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
function map_colnames($input){
global $colnames;
return isset($colnames[$input])? $colnames[$input]:$input;
}
// filename for download
$filename = "website_data_" . date('Ymd') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
$flag = false;
$result = mysql_query("SELECT * FROM futurehomeowner1 ORDER BY id") or die(mysql_error());
while(false !== ($row = mysql_fetch_assoc($result))) {
if(!$flag) {
// display field/column names as first row
$firstline=array_map('map_colnames',array_keys($row));
fputs($firstline,',','"');
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
array_walk($row, 'cleanData');
echo implode("\t", array_values($row)) . "\r\n";
}
exit;
?>
It is working fine and getting data from database to excel. I want to use header names as my own not a database column names. The problem is that when the excel file downloads there is no header names and showing this message "Warning: fputs() expects parameter 1 to be resource, array given
". Below of this message data is being showing correctly but in one column.
Upvotes: 0
Views: 475
Reputation: 1773
You used fputs
in a wrong way, you should pass a reference to the file then the string. First you should open a file handler to your filename to pass it later.
<?PHP
include '../connection.php';
$colnames = array(
"fname" => "First Name",
"lname" => "Last Name",
"email" => "Email Address",
"phone" => "Phone No.",
"timeFrom" => "Time From",
"timeto" => "Time to",
"comments" => "User Comments"
);
function cleanData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
function map_colnames($input){
global $colnames;
return isset($colnames[$input])? $colnames[$input]:$input;
}
// filename for download
$filename = "website_data_" . date('Ymd') . ".xls";
$handler = fopen($filename, "w+");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
$flag = false;
$result = mysql_query("SELECT * FROM futurehomeowner1 ORDER BY id") or die(mysql_error());
while(false !== ($row = mysql_fetch_assoc($result))) {
if(!$flag) {
// display field/column names as first row
$firstline=array_map('map_colnames',array_keys($row));
fputs($handler, implode("\t",$firstline));
$flag = true;
}
array_walk($row, 'cleanData');
fputs($handler, implode("\t", array_values($row)) . "\r\n");
}
exit;
?>
have a look at php manual
Upvotes: 1