0

Exporting the data into a csv file is working correctly but the problem when trying to open the csv file displaying a line gap between each record.

Added this line because earlier if i open the file in notepad it is displaying all the data in a single line so added this line after that it working fine in notepad but faced problem in CSV format getting a line gap before each record.

fwrite($output,"\r\n"); 


$txtstartdate=$_POST['txtstartdate'];
    $txtenddate=$_POST['txtenddate'];   

    header('Content-Type:text/csv;charset=utf-8');
    header('Content-Disposition:attachment;filename=appointmentlist.csv');
    $output = fopen('php://output','w');
    fputcsv($output,array('Appointment Id','First name','Last Name','Email','Gender','Department','Phone Number','Appointment Date','Address','Status'));
    $query = "select * from appointment  WHERE date BETWEEN '$txtstartdate' AND  '$txtenddate' ORDER BY date DESC";
    $result = mysqli_query($conn,$query);
    while($row = mysqli_fetch_assoc($result))
    {
        fwrite($output,"\r\n"); 
        fputcsv($output,$row);
    }
    fclose($output);

If i open in Notepad it is displaying like this

NOTEPAD:

"Appointment Id","First name","Last Name",Email,Gender,Department,"Phone Number","Appointment Date",Address,Status

2,aaa,bbbb,ccc@gmail.com,male,physician,9606567652,2018-10-04,"ctc,odisha",0

3,nas,maha,shu@gmail.com,male,physician,9207644052,2018-10-04,"ctc,odisha",0

In CSV it is displaying one row gap after each record.So i don't want to display gap after each record in CSV file.

Johan
  • 3,577
  • 1
  • 14
  • 28
test
  • 11
  • 4
  • If I'm not mistaken, fputcsv add a line terminator by itself. No need of writing \r\n to the file manually. – DigiLive Oct 24 '18 at 07:40
  • @dnFer if i am not adding that then it is displaying all the records in a single line in notepad for that purpose i have added that line – test Oct 24 '18 at 08:26
  • Possible duplicate of [fputcsv and newline codes](https://stackoverflow.com/questions/4080456/fputcsv-and-newline-codes) – DigiLive Oct 24 '18 at 10:35

1 Answers1

1

You are getting gaps because of

  fwrite($output,"\r\n");

line. fputcsv will add new line character automatically at the end of the line, so no need to add another one. According to fputcsv documentation:

fputcsv() formats a line (passed as a fields array) as CSV and write it (terminated by a newline) to the specified file handle.

(emphasis is mine)

UPDATE: fputcsv always uses "\n" as the line ending. Almost all windows applications understand it correctly except for Notepad up until Windows 10. The Windows 10 version now does render it correctly. If you are absolutely set on using \r\n as the line ending, you'll need additional trickery - look at fputcsv and newline codes for more info.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • If i am not adding that line then in CSV format it is working fine but if i try to open the file in notepad it is displaying the records and headings all in one single line only – test Oct 24 '18 at 08:25
  • Don't open it in notepad. Use a normal editor it even Excel – Aleks G Oct 24 '18 at 08:26
  • @test They don't need "notepad format" - they need text format, which you get. Notepad simply doesn't understand "\n" as the line ending. – Aleks G Oct 24 '18 at 10:39