2

I'm exporting a some data from a survey to CSV for use by another team with PHP. I have outputted my first row of data as all of the headings. But I need some of the headings to span 2 columns is the at all possible.

The headings are all in correctly, but I need to add two cells of data underneath some of the headings.

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=lite-survey-data_'.time().'.csv');

require_once ( dirname(dirname(dirname(dirname(__FILE__)))).'/wp-load.php' );

$content =build_csv_download();
$headings = convert_to_csv($content[0]);
$csv_content = convert_to_csv($content[1]);
$output = fopen('php://output', 'w');
fwrite($output, $headings);
fwrite($output, $csv_content);

Any ideas?

0

3 Answers 3

1

It's not possible to have headings that span two columns as CSV is such a basic format it doesn't allow you to represent merged cells. However if the second of the two columns has no header (so heading,,nextheading) then when you open it in Excel the heading will spread across into the empty cell.

Sign up to request clarification or add additional context in comments.

1 Comment

This could be the answer i'm looking for. I forget how primitive .csv can be at times. Just trying it now. Cheers for the suggestion though.
0

CSV does not support column spanning. Some programs like Excel might try to make sense out of missing headings or other such tricks, but that doesn't leave you with a valid CSV file. See https://www.rfc-editor.org/rfc/rfc4180

Concatenate the data with a non-significant string (like a space or a hyphen) before making the CSV data. Basically, you're just taking the two data columns and making them in to one.

The easiest way to do this is to use CONCAT (docs) to combine the stings right out of the database:

    SELECT 
        id,
        CONCAT(first_name, " " last_name) AS name,
        email
    FROM 
        users

 /*   Gives these results:
    Array
    (
        [0] => Array
            (
                [id] => 1
                [name] => Bob Someuser
                [email] => [email protected]
            )
    
        [1] => Array
            (
                [id] => 2
                [name] => George Anotheruser
                [email] => [email protected]
            )
    
    )
*/

If you can't do that in the database query, you can use array_walk (docs) to do it after the fact:

$headings = array(
    'First heading',
    'Second heading',
    'Third heading'
);

$one_data = array(
    'First data',
    'Second',
    'data',
    'Third data'
);
$data = array();
for ($x=0;$x<10;$x++)
    $data[] = $one_data;

print 'Before fix:';
print_r($data);

function prepare_array (&$item) {
    $item[1] = $item[1].' '.$item[2];
    unset($item[3]);
}
array_walk($data, "prepare_array");

print '<br><br>After fix:';
print_r($data);

function csv_array (&$item) {
    $item = implode(',', $item);
}
$csv = implode(',', $headings)."\n";
array_walk($data, "csv_array");
$csv .= implode("\n", $data);

print '<br><br>CSV:<br>'.$csv;

Comments

0

CSV doesn't support column-spanning, so you'd just have to put the same field header on top of two columns. It's not the prettiest thing in the world, but then CSV isn't the most flexible format in the world either.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.