Jump to content

[SOLVED] Generating a CSV on the fly


bluesoul

Recommended Posts

Ugh, having some problems getting this working.

 

Before my loop:

 

$list = array (
    'Account ID,Invoice ID,Domain,Invoice Date, Registrar, Description, Exp. Date, Charge, Balance');

 

In the loop:

$fp = fopen('invoice'.$timestamp.'.csv', 'w');
$list .= array (
    "$aaid,$aiid,$adn,$invdate, $arid, $adesc, $expdate, $charge, $balance");
foreach ($list as $line) {
    fputcsv($fp, split(',', $line));
}

 

After the loop:

fclose($fp);

 

Results in "Warning: Invalid argument supplied for foreach() in searchinvoices.php on line 77".  I'm assuming it's because I can only pass it one element at a time.  Ideas?

Link to comment
https://forums.phpfreaks.com/topic/140535-solved-generating-a-csv-on-the-fly/
Share on other sites

You do not want to use the "." operator, try something like this:

<?php
$list = array();
$list[] = array (
    'Account ID,Invoice ID,Domain,Invoice Date, Registrar, Description, Exp. Date, Charge, Balance');
$fp = fopen('invoice'.$timestamp.'.csv', 'w');
$list[] = array (
    "$aaid,$aiid,$adn,$invdate, $arid, $adesc, $expdate, $charge, $balance");
foreach ($list as $line) {
    fputcsv($fp, split(',', $line));
}?>

 

Ken

$fp = fopen('invoice'.$timestamp.'.csv', 'w');
$list = array (
    'Account ID','Invoice ID','Domain','Invoice Date','Registrar','Description','Exp. Date','Charge','Balance');
fputcsv($fp,$list);

$list = array (
    $aaid,$aiid,$adn,$invdate,$arid,$adesc,$expdate,$charge,$balance);
fputcsv($fp,$list);

couple of things...

-the fopen should probably be OUTSIDE the loop

-you are putting variables into a string then back to an array...just put them right into the array:

$list = array ($aaid,$aiid,$adn,$invdate, $arid, $adesc, $expdate, $charge, $balance);
fputcsv($fp, $list);

$fp = fopen('invoice'.$timestamp.'.csv', 'w');
$list = array (
    'Account ID','Invoice ID','Domain','Invoice Date','Registrar','Description','Exp. Date','Charge','Balance');
fputcsv($fp,$list);

$list = array (
    $aaid,$aiid,$adn,$invdate,$arid,$adesc,$expdate,$charge,$balance);
fputcsv($fp,$list);

 

This got it, thanks a ton, I'd have been beating my head on this for a while.

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.