PC Nerd Posted December 18, 2007 Share Posted December 18, 2007 Hi, Im looking for a way to write to a CSV file. I can get the writing working, but i cant seem to find out how to get the records seperated. at the moment the firlds are placed correctly "value, value, value" - however the next record only continues on the same line when veiwed in MS Office Excel. How can I create the newline? thanks Link to comment https://forums.phpfreaks.com/topic/82120-solved-csv-writing/ Share on other sites More sharing options...
PC Nerd Posted December 18, 2007 Author Share Posted December 18, 2007 *** UPDATE: echoing: "\n"; at the end of the record foesnt work - it actually echos out as the characters "\" and "n". is this because o fht encoding or somethign eg ASCI etc??? thanks Link to comment https://forums.phpfreaks.com/topic/82120-solved-csv-writing/#findComment-417405 Share on other sites More sharing options...
btherl Posted December 18, 2007 Share Posted December 18, 2007 Try echoing "\n" instead of '\n'. Double quotes are necessary for those special characters. Link to comment https://forums.phpfreaks.com/topic/82120-solved-csv-writing/#findComment-417436 Share on other sites More sharing options...
PC Nerd Posted December 18, 2007 Author Share Posted December 18, 2007 <?php $lastname = "ME"; $firstname = "NAME"; $email = "[email protected]"; if(is_writable('TEST.csv')) { $fp = fopen('TEST.csv','a'); $content = "$lastname,$firstname,$email\n"; fwrite($fp,$content); fclose($fp); } ?> thats what ive got - and its working. Thanks Thanks Link to comment https://forums.phpfreaks.com/topic/82120-solved-csv-writing/#findComment-417445 Share on other sites More sharing options...
~n[EO]n~ Posted December 18, 2007 Share Posted December 18, 2007 I changed this and it is working for me, you should give a try $content = "$lastname,$firstname,$email" . "\r\n"; Link to comment https://forums.phpfreaks.com/topic/82120-solved-csv-writing/#findComment-417450 Share on other sites More sharing options...
btherl Posted December 18, 2007 Share Posted December 18, 2007 That looks fine then.. how did you find that it was printing \ and n instead of \n? Oh, and try neon's suggestion \r\n is the windows end-of-line Link to comment https://forums.phpfreaks.com/topic/82120-solved-csv-writing/#findComment-417451 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.