I found the following script on the web and edited it to my needs, but the one thing I’m not able to figure out is how to change the column names. AS-is, the column names show up in the spreadsheet as the name of the corresponding column in the database. is there a way to change this?
thanks for any advice!
here’s my code:
<?php
$host = 'asdf';
$user = 'asdf';
$pass = 'asdf';
$db = 'asdf';
$table = 'stats';
$file = 'export';
$fields = "date, name, time, inbset, asapset, faxset, obset, totset, inbclo, asapclo, faxclo, obclo, totclo, inbtot, asaptot, faxtot, obtot, total, comments";
$filename = $file."_".date("Y-m-d");
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].", ";
$i++;
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT $fields FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>
The column names are coming from the first row in your CSV output.
Around line 20:
$csv_output .= $row['Field'].", ";Simply remove that block of code, and output a line of column names like you wish:
“Column Name 1, Column Name 2, Column Name 3, …”