Jump to content

Writing an Array to a CSV


Benny150592
Go to solution Solved by Ch0cu3r,

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

Edited by Benny150592
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.