Re: CSV Files

From: Date: Thu, 23 Oct 2008 06:22:21 +0000
Subject: Re: CSV Files
References: 1 2 3 4 5 6  Groups: php.general 
Request: Send a blank email to php-general+get-282328@lists.php.net to get a copy of this message
On Wed, 2008-10-22 at 22:30 -0400, Andrew Ballard wrote: > On Wed, Oct 22, 2008 at 10:15 PM, Jason Todd Slack-Moehrle > <mailinglists@mailnewsrss.com> wrote: > > On Oct 22, 2008, at 6:58 PM, Stut wrote: > > > >> On 23 Oct 2008, at 02:41, Jason Todd Slack-Moehrle wrote: > >>> > >>> Actually i am ending the row headers with a chr(10); // LINE FEED > >> > >> From the code you included in your original post... > >> > >>> echo "/n"; > >> > >> > >> There was no mention of chr(10). > >> > >> Outputting data in CSV format is not hard. Simply echo the header row if necessary, > >> followed by "\n". Then output each line taking care to put string values in quotes which > >> means you also need to escape quotes in the data. After each line echo "\n". That's > >> really all there is to it. > >> > >> If you're still having problems I suggest you post the exact code you're > >> using, anything else just makes it harder for us to provide effective help. > >> > >> -Stut > >> > >>> On Oct 22, 2008, at 5:12 PM, Stut wrote: > >>> > >>>> On 23 Oct 2008, at 00:59, Jason Todd Slack-Moehrle wrote: > >>>>> > >>>>> After I right out the column headers do I have to put a '/n' to > >>>>> have it start a new line in the CSV file? I think I do. > >>>> > >>>> A new line is \n not /n, and it must be in double quotes (") not single > >>>> ('). > >> > > > > Oh, I am not putting quotes around each field that i get from MySQL. There are no quotes > > in the data so that is good. > > > > Sorry I put "/n" and I meant to put chr(10). > > > > -Jason > > > > Jason, one of the points that Stut was trying to explain is that "\n" > and chr(10) are the same thing. They are just two different ways to > refer to a newline (line feed) character. Most of us probably use "\n" > rather than chr(10) in PHP, though. So, the following two lines are > equivalent: > > <?php > echo "Item1, Item2, Item3" . chr(10); > > // Note this uses double quotes. > echo "Item1, Item2, Item3\n"; > > // It won't be the same at all if you use single quotes > echo 'Item1, Item2, Item3\n'; > > ?> > > At any rate, you are correct that you need a line feed/newline > character at the end of every row in CSV including the header row. > > Andrew > A line feed and \n are not the same thing at all. Different operating systems implemented a different method of line endings depending on what they thought best, either a carriage return or line feed or both. a \n is meant to be an OS agnostic way of implementing this in the various programming languages, outputting something that any OS can understand in the intended way. Ash www.ashleysheridan.co.uk

« previous php.general (#282328) next »