fputcsv - only one column

10,266

Solution 1

This this:

fputcsv($fp, array_values($list));

EDIT:

Use this to get it instantly working with excel:

fputcsv($fp, array_values($list), ';', ' ');

EDIT 2: The default character that is used as the field-separator in excel is set by the locale settings of windows. That means: If you export a csv-file with german locale settings will cause problems with excel in an us-environment... Another thread covered this topic.

Solution 2

Try this

fputs($fp, implode(array_keys($list), ',')."\n");

fputs($fp, implode(array_values($list), ',')."\n");
Share:
10,266
Matthias
Author by

Matthias

Updated on June 15, 2022

Comments

  • Matthias
    Matthias almost 2 years

    I want to save parts (and differently arranged) of my mysql database in a CSV file. For that I wrote this piece of code:

    $headerDisplayed = false;
    foreach ($arr as &$currName)
    {
        $result2 = mysql_query("SELECT * FROM Experiment Where Person='$currName'"); 
        $i = 1;
        while ($row2 = mysql_fetch_array($result2)) 
        {
            $list['Name'] = $currName;
            $list['Studie'] = $row2['Studie']; 
            if(strcmp($row2['Antwort'],$row2['Korrekt']) == 0)
            {
                $list["A $i"] = '0';
            }
            else
            {
                $list["A $i"] = '1';
            }
    
            $i++;
        }
    
        if ( !$headerDisplayed ) {
            // Use the keys from $data as the titles
            fputcsv($fp, array_keys($list));
            $headerDisplayed = true;
        }
    
        fputcsv($fp, $list);
    }
    
    fclose($fp);
    

    Unfortunately this does not what I wanted it to do. My plan was that each entry of $list (e.g. $list["A 1"] or $list["A 15"]) has its own column. And then the call of fputcsv should create a new line and again one column for each $list entry. I create new rows with each fputcsv call, just as expected, but the complete $list array is in the first (most left) column.

    What do I have to do to solve this issue?

    ADDED: I get something like:

    [1,0,0,0,1,1,0,0] [] [] [] [] [] [] []
    
    [1,0,1,1,0,1,0,0] [] [] [] [] [] [] []
    
    [0,1,1,1,1,1,0,1] [] [] [] [] [] [] []
    

    but what I want is more like:

    [1] [0] [0] [0] [1] [1] [0] [0]
    
    [1] [0] [1] [1] [0] [1] [0] [0]
    
    [0] [1] [1] [1] [1] [1] [0] [1]
    
  • Matthias
    Matthias almost 12 years
    This only leads to [Array] in each row of the csv file :(
  • Ron
    Ron almost 12 years
    Bad idea! What, if the input is somethink like: "Surname, Firstname" ?
  • Matthias
    Matthias almost 12 years
    That would not be the case, even $list['Name'] is in fact not a name but a coded integer number referring to a name. But still this doesn't change anything. I add something to my question.
  • Ron
    Ron almost 12 years
    Your code just handles the header-column. If you apply this on the csv-content, a comma in a field could possibly occure...
  • Matthias
    Matthias almost 12 years
    I can run it, and it returns a CSV file which has "This is a test,"""This"" is a test","This is a 'test'","This is a" in its first row first column and then "test" in next row. And this repeated a lot of times.
  • Ron
    Ron almost 12 years
    @Justin Why whould one use array_map together with fputcsv just wo wrap "" aroung fields? Just use fputcsv($f, $array, ';', '"');
  • Matthias
    Matthias almost 12 years
    I tested it anyway. It messes everything completely up^^ But thank you very much for your trys and your efford :)
  • Jinzhao Huo
    Jinzhao Huo over 11 years
    Cool! change delimiter to ";" works perfect! Thank you very much! Still I am curious why using "," as the delimiter doesn't work???
  • commonpike
    commonpike over 10 years
    Why is there a ' ' space for enclosure ?