2

I want to parse text from my database into a CSV-file with PHP. The problem is that nothing is parsed to the file.

function exportToCSV($output)
{
    if(isset($output))
    {
        $fp = fopen("skrivtest/test.csv", "w");
        foreach($output as $out) 
        {
            echo $out . "<br>";
            fputcsv($fp, $out); 
        }
        fclose($fp);
        return true;
    }
    else    
    {
        return false;
    }
}

if($export_csv)
{
    if(!exportToCSV($output = array($row->fornamn . " " . $row->efternamn . " " . $row->personnr . " " . $row->gatuadress . " " . $row->postnr . " " . $row->postadress, '')))
    {
        echo "Can't reach function";
        exit;
    }
}

The output from var_dump($output)

array(2) { [0]=> string(62) "Helene Schultzberg 520622-2902 Fröseke Nygärdet 360 77 FRÖSEKE" [1]=> string(0) "" } Helene Schultzberg xxxx Fröseke Nygärdet 360 77 FRÖSEKE

array(2) { [0]=> string(61) "Dan Johansson 560122-2754 Fibblegård Herråkra 360 73 LENHOVDA" [1]=> string(0) "" } Dan Johansson xxxxx Fibblegård Herråkra 360 73 LENHOVDA

array(2) { [0]=> string(55) "Owe Lindahl 600521-2797 Östraby Kvarn 2 360 73 LENHOVDA" [1]=> string(0) "" } Owe Lindahl xxxx Östraby Kvarn 2 360 73 LENHOVDA

array(2) { [0]=> string(56) "Madelene Runberg 680302-2703 Östervik 14 360 73 LENHOVDA" [1]=> string(0) "" } Madelene Runberg xxxx Östervik 14 360 73 LENHOVDA

array(2) { [0]=> string(48) "Bo Söderberg 420818-0077 Änghult 360 73 LENHOVDA" [1]=> string(0) "" } Bo Söderberg xxxxxx Änghult 360 73 LENHOVDA

user500468
  • 1,183
  • 6
  • 21
  • 36
  • 1
    In your case `$output` must be 2d array – hindmost Mar 12 '14 at 09:42
  • you might consider using seperator for csv. At least some semicolon. spaces are not very save since they might appear in strings and mess up your data that way. Read up on CSV if you have the time – Andresch Serj Mar 12 '14 at 09:45
  • But how should I do to get the text into the CSV-file? – user500468 Mar 12 '14 at 09:46
  • 2
    @user500468 `fputcsv` expects 2nd parameter to be array, in your code `string` is passed – hindmost Mar 12 '14 at 09:47
  • Well. Why is there a if($export_csv)? the $export_csv never gets filled now does it? also replace fputcsv($fp, $out); with fputcsv($fp, explode(' ',$out)); – Andresch Serj Mar 12 '14 at 09:47
  • Post **`var_dump($output)`** in you question – Ijas Ameenudeen Mar 12 '14 at 09:48
  • $export_csv contains a boolean value if the user has choosed to parse the search result or not. It is a checkbox. How can I make the second parameter to an array? – user500468 Mar 12 '14 at 10:08
  • I have now changed my code: http://pastebin.com/3zLaYntp Now it is only one row inserted into the file, the LAST line in the search result. I have no idea why. Anyone? – user500468 Mar 12 '14 at 10:20

3 Answers3

1

Why you are doing it in PHP side when you can do it in SQL side?

SELECT * INTO OUTFILE "c:/mydata.csv"
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY "\n"
FROM my_table;

(the documentation for this is here)

HMagdy
  • 3,029
  • 33
  • 54
0
$output = array($row->fornamn . " " . $row->efternamn . " " . $row->personnr . " " . $row->gatuadress . " " . $row->postnr . " " . $row->postadress, ''

should be replaced with

$output = array($row->fornamn, $row->efternamn, $row->personnr, $row->gatuadress, $row->postnr, $row->postadress)
keepwalking
  • 2,644
  • 6
  • 28
  • 63
0

$fp = fopen("filename.csv","w"); $value = array($var1,$var2,$var3); fputcsv($fp,$value,"\t");