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
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

Link to comment
Share on other sites

$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);

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

$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.

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.