0

My csv download code run correctly but i want to add title in first row of csv.But I can't.

My code

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);
}
Ahmed Siouani
  • 13,064
  • 11
  • 59
  • 70

1 Answers1

0

Create an array before dumping data into that array, like below:

$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
}
Suresh Kamrushi
  • 13,699
  • 12
  • 70
  • 84