我的csv下载代码运行正常,但我想在csv的第一行添加标题。但我不能。
我的代码
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
include('config.php');
$sql ="select county.title,beach.beach_name,beach.notice,beach.latitude,beach.longitude,beach.rainfall,beach.temperature,beach.status_id from beach as beach,county as county where beach.county_id=county.id ";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc( $result)) {
$data[] = $row; // Inside while loop
}
outputCSV($data);
function outputCSV($data) {
$output = fopen("php://output", "w");
foreach ($data as $rowc) {
fputcsv($output, $rowc);
}
fclose($output);
}
答案 0 :(得分:0)
在将数据转储到该数组之前创建一个数组,如下所示:
$sql ="select county.title,beach.beach_name,beach.notice,beach.latitude,beach.longitude,beach.rainfall,beach.temperature,beach.status_id from beach as beach,county as county where beach.county_id=county.id ";
$data[] = array("title","Beach Name","Notice","Latitue","Longitude","RainFall","Temperature","Satus ID");
$result = mysql_query($sql);
while( $row = mysql_fetch_assoc( $result)){
$data[] = $row; // Inside while loop
}