Jump to content

Writing an Array to a CSV


Benny150592

Recommended Posts

Hi Guys,

 

I'm trying to write an array to a CSV file and having no luck using fputcsv. The array I'm getting back contains the information and it should be being written to the database every time the for loop goes round, but all I'm getting is an empty CSV. Any ideas?

$fileName =  date('d m Y') . '.csv';
$fp = fopen($fileName, 'w');
header('Content-Description: File Transfer');
header('Content-Type: text/csv');
header("Content-Disposition: attachment; filename=\"$fileName\"");
$csvData = array();
for($j =0; $j < $messageAmount; $j++){
$messageNumber = $message->messages[$j]->number;
$messageBody = $message->messages[$j]->message;
$messageDate = $message->messages[$j]->date;
$csvData[$j] = array($messageNumber,$messageBody,$messageDate);
    fputcsv ($fp, $csvData[$j]);
}

fclose($fp);
?>

This is the code I'm using btw.

 

Regards,

Ben

Link to comment
https://forums.phpfreaks.com/topic/282953-writing-an-array-to-a-csv/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.