0

So I'm trying to make a .csv file which after I will download it,but the problem is that the rows are not going aranging properly. I am using fputcsv.

$tot.="$codcomanda,$clientnume,$brandnume,$numeprod,$um,$cantitate,$updatare";
$list =array(
    array('Comanda','Client','Categorie','Produs','UM','Cantitate','Actualizare'),
    array($tot),
);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);

The variable $tot is from a while statement and it gets its variable from different queries and I cannot find a php code on the internet that is for my needs.I've tried several ways to try and make it work but nothing. I've tried making $tot different ways but none work,either it $tot.= The csv file should have something like.

  • Title line,
  • Line 1,
  • Line 2,
  • etc

But my csv shows like

  • Title line,
  • "Line1,Line2,etc"

I've tried making $tot different ways but none work,either the csv show like this or doesn't display no content at all.


Solved: Using fwrite i've solved it.

$tot.="$codcomanda;$clientnume;$brandnume;$numeprod;$um;$cantitate;$updatare;\n";
$tot2="Comanda;Client;Brand;Produse;U.M;Cantitate;Actualizare;\n$tot";

    $file = fopen("file.csv","w");
    fwrite($file,$tot2);
    fclose($file);

And it write's it like it should.

4
  • do you by any chance try to view this on Windows with something as simple as notepad? there's a good chance that your CSV is linux formatted, which uses different line endings. A good editor would show them as separate lines if it's this simple. Commented Feb 21, 2013 at 15:22
  • Same on notepad and MS excel 2012.And yes indeed im using ubuntu 12.04 with Gedit as text editor and Libreoffice Calculator. Commented Feb 21, 2013 at 16:00
  • Kindly download and check on notepad++ notepad-plus-plus.org Commented Feb 21, 2013 at 16:05
  • First of all thanks for your time, same stuff is showing in notepad++, the problem is that i need to make array($tot) see it for every line to make it something like array($line1),array($line2)...etc.If i set $tot.=($codcomanda,$clientnume,$brandnume,$numeprod,$um,$cantitate,$updatare); and at $list i set only $tot there is no result. Commented Feb 21, 2013 at 16:28

2 Answers 2

1

Try like the following example

     <?php
          $list =array(
                       array('Comanda','Client','Categorie','Produs','UM','Cantitate','Actualizare'),
           array('P1','C1','CL1','2','2','Ct1','A1'),
           array('P2','C2','CL2','2','2','Ct2','A2'),
          );

       $fp = fopen('file.csv', 'w');

     foreach ($list as $fields) {
     fputcsv($fp, $fields);
    }

     fclose($fp)
   ?>
Sign up to request clarification or add additional context in comments.

3 Comments

you need to specify each lines in your csv as array,Or you can download a csv by setting headers and using implode() function
yeah but the problem is I cannot make different lines as arrays,only all lines as one array.I managed to resolve using fwrite.
Yeah,updated my post.Thank you for your interventions.I cannot set your answer as usefull since i have 1 reputation.Hopefully I will find a way in the future to write my code with fputcsv.
0

Try to add this line before the calling of fopen(): ini_set("auto_detect_line_endings", true)

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.