Question

I'm trying to export some data into a csv file, I've got the data going into the file, this is my code:

*Please excuse me if the code is bad, I've never done anything like this and this was the only way I could get it working.

    /* CSV Export - Create Row */
    $csv_row_content .= $userdata->id.',';
    $csv_row_content .= $userdata->firstname.' '.$userdata->lastname;
    $csv_row_content .= ','.$split_date[2].'-'.$split_date[1].'-'.$split_date[0].',';
    $csv_row_content .= $sale->name;
        if($sale->optionlabel):
            $csv_row_content .= ' ('.$sale->optionlabel.')';
        endif;
    $csv_row_content .= ',';
    $csv_row_content .= $sale->status.',';
    $csv_row_content .= number_format($sale->unitprice, 2, '.', '');
    $csv_row_content .= "\r\n";

    $data_array[] = $csv_row_content;

    $csv_file = fopen('../wp-content/plugins/data-export/export_doc.csv', 'w');
    foreach ($data_array as $single_line)
        {
    fputcsv($csv_file,split(',',$single_line));
        }
    fclose($csv_file);

    /* Clear Array */
    unset($data_array);
    $data_array = array();

It's working except I'm having trouble with the quotations marks on certain items

    303,"User Name",12-02-2013,"College Sweater (Black)",,"20.00
    207","User Name",30-01-2013,"College Sweater (Black)",,"20.00
    "

So I'm not sure what the go is with the first and last items, the one quotation mark show up sometimes and not in others. Notice the odd quotation mark on row id 207 & on the last value for both row. Also there's a new line begin made on the third row with just a single quote.

Also on some other items the function is splitting the name of the item into two items. eg:

    207","User Name",30-01-2013,"College ","Sweater (Black)",,"22.73

So obviously I'm off base here somewhere, if anyone could help me with this, I'm really keen on learning the correct way this kind of thing should be done, checked the php.net docs quite a bit, but a lot of the time I find that resource incredibly overwhelming and this is one such occasion.

If anyone can point me in the right direction on this I'd really appreciate it. I'd prefer to understand this than just have a copy and paste solution.

Thanks, Frank

Was it helpful?

Solution

You are manually creating a CSV string and splitting it before using fputcsv to put it back together. Try this instead:

$row = array(
    $userdata->id,
    $userdata->firstname . ' ' . $userdata->lastname,
    $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0],
    $sale->name . ($sale->optionlabel ? $sale->optionlabel : ''),
    $sale->status,
    number_format($sale->unitprice, 2, '.', '')
);

$data[] = $row;

$csv_file = fopen('../wp-content/plugins/data-export/export_doc.csv', 'w');

foreach ($data as $line) {
    fputcsv($csv_file, $line);
}
fclose($csv_file);

This creates an array containing all the fields which can be passed to fputcsv which takes care of enclosing and delimiting fields as well as line endings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top