I am trying to create a CSV file. I have done this. I have put the below in a loop with the first and last lines outside of the loop.
$FileHandle = fopen('tech.csv', 'a+') or die("can't open file");
$stringa = $item." , ".$item2."\r\n";
fwrite($FileHandle, $stringa);
fclose($FileHandle);
However, it comes out like this in the CSV file:
a
b
c
d
Rather than the way I want it:
a b
c d
Basically, two columns rather than one.
What am I doing wrong?
Do you read those values from a file/stream using fgets()? Then the trailing linebreak is part of the string. Use trim() to remove the linebreak.
You might also be interested in the function fputcsv().