2

I am trying to convert sql query returned array in CSV file, but i am experiancing a wierd behavior of my code since 1st line of my csv file is coming out to be blanck and also line containing column headers is starting after two blocks. following is my code,

$csv = "name,job,salary,age \n";
$export = $this->model2->emp_data();
foreach ($export as $key => $value) { 
                $csv.=  $value['name'].','. $value['job'].','. $value['sal'].','.$value['age']."\n";
}
$filename ='excel.csv';
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=" . $filename);
header("Pragma: no-cache");
header("Expires: 0");
$csv_handler = fopen('php://output', 'w');            
fwrite ($csv_handler,$csv);
fclose ($csv_handler);

$csv is giving me my header names, $export is the sql returned data what am i oing wrong here ? thank you

sample input (query returned data) -

[0] => Array
        (
            [name] => jeff1
            [job] => DBA
            [sal] => 6999
            [age] => 44
            [create] => test
            [update] => test

        )

    [1] => Array
        (
          [name] => jeff2
            [job] => DBA
            [sal] => 6999
            [age] => 44
            [create] => test
            [update] => test
        )
        ....so on

output - https://drive.google.com/open?id=1u0Q8OgerLAGlq_r610W7KU0N0hnu0gZE

shellbot
  • 43
  • 7
  • could you add a sample of input and of current output? – Jeff Oct 07 '18 at 14:19
  • heyy jeff thank you for quick responce check the question i just did – shellbot Oct 07 '18 at 14:29
  • Don't output your lines just by joining fields with commas -- it will break as soon as you get a field with a comma in it. Instead use [`fputcsv()`](http://php.net/manual/en/function.fputcsv.php) which will do all the proper escaping for you. – Alex Howansky Oct 07 '18 at 14:33
  • thank you @AlexHowansky i have been meaning to update the code to use fputcsv() .. will this solve the problem i am facing currently with the headers? – shellbot Oct 07 '18 at 14:35
  • What is your PHP version and server details ? I also face the same issue. This started happening after upgrading to PHP 7 – Madhur Bhaiya Oct 07 '18 at 16:09
  • Try opening that CSV using some text editor (not Excel), and check if still there is a blank line at the beginning – Madhur Bhaiya Oct 07 '18 at 16:11
  • Check: https://www.ozonesolutions.com/programming/2012/06/php-whitespace-issues-when-exporting-to-csv-files/ – Madhur Bhaiya Oct 07 '18 at 16:15
  • @MadhurBhaiya yes there is an empty space , but i dont understand where it is coming from? – shellbot Oct 07 '18 at 16:50
  • i checked the function where i have wrote dowload method but there no empty lines there, is there any way of ignoring these white spaces – shellbot Oct 07 '18 at 16:54
  • You might want to understand and try [this post's solution](https://stackoverflow.com/questions/4249432). – Stock Overflaw Oct 07 '18 at 17:33

0 Answers0