4

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]
Taz
  • 3,718
  • 2
  • 37
  • 59
Matthias
  • 1,200
  • 2
  • 13
  • 33
  • So, where do you get this "result" gets displayed this way? Excel? – Ron Jul 18 '12 at 12:47
  • 1
    Ok, then this is your problem. Use the 3rd and 4th parameter of fputcsv() to get your CSV format fit to excel. (Dont know what excel accepts immediately) – Ron Jul 18 '12 at 12:51
  • That helped so much, thank you. All I had do do was changing to "fputcsv($fp, $list, ';',' ');" ... Is it possible to give you a +1 for this comment? Or which one do I mark as answer? – Matthias Jul 18 '12 at 13:06
  • I made the change to my answer below... – Ron Jul 18 '12 at 13:16

3 Answers3

10

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.

Community
  • 1
  • 1
Ron
  • 1,336
  • 12
  • 20
  • 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. – Matthias Jul 18 '12 at 12:49
  • Cool! change delimiter to ";" works perfect! Thank you very much! Still I am curious why using "," as the delimiter doesn't work??? – Jinzhao Huo Sep 03 '12 at 03:29
  • Why is there a ' ' space for enclosure ? – commonpike Sep 24 '13 at 12:34
1

Try this

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

fputs($fp, implode(array_values($list), ',')."\n");
Justin John
  • 9,223
  • 14
  • 70
  • 129
  • Bad idea! What, if the input is somethink like: "Surname, Firstname" ? – Ron Jul 18 '12 at 12:36
  • 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. – Matthias Jul 18 '12 at 12:40
  • Your code just handles the header-column. If you apply this on the csv-content, a comma in a field could possibly occure... – Ron Jul 18 '12 at 12:45
  • @Justin Why whould one use array_map together with fputcsv just wo wrap "" aroung fields? Just use fputcsv($f, $array, ';', '"'); – Ron Jul 18 '12 at 13:19
  • I tested it anyway. It messes everything completely up^^ But thank you very much for your trys and your efford :) – Matthias Jul 18 '12 at 13:31
0

You have to do a matrix:

        {
            $list["A"]["$i"] = '0';
        }
        else
        {
            $list["A"]["$i"] = '1';
        }

See example 1 of http://php.net/manual/en/function.fputcsv.php

Pablo Martinez
  • 2,172
  • 1
  • 23
  • 27