Re: Send .csv file to browser [message #170019 is a reply to message #170014] |
Tue, 05 October 2010 18:02 |
sheldonlg
Messages: 166 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 10/5/2010 12:05 PM, Jerry Stuckle wrote:
> On 10/5/2010 10:55 AM, sheldonlg wrote:
>> On 10/5/2010 10:20 AM, Derek Turner wrote:
>>> On Tue, 05 Oct 2010 14:19:04 +0000, Derek Turner wrote:
>>>
>>>
>>>>
>>>> 3. Use appropriate header files and then readfile()
>>>
>>> Sorry lines/statements
>>
>> Why not simply
>>
>> $theData = getDataFromTableAsArrayOfNonAssociativeArrays();
>> foreach ($theData as $rec) {
>> $line = implode(',', $rec);
>> print $line . '<br>';
>> }
>>
>> The user can then cut and paste it into a file or you can put in code
>> for the user to download that piece.
>>
>> Another approach is to output an Excel file and the user can then output
>> from that Excel file to a .csv file.
>>
>>
>
> Right idea, but some problems. First of all, what happens if a field
> contains a comma i.e. "Smith, Jr.". Text fields also need to be
> surrounded by double quotes, and any double quotes in the text field
> must be doubled to be a valid CSV file. Also, some places use a
>
> Not hard - just a matter of looking at each field, and if it is numeric,
> insert it in the stream. If it is text, replace all occurrences of '"'
> with '""' and insert it with surrounding quotes. Of course, use commas
> to separate the fields.
>
> Then just output directly with the appropriate headers.
>
Thanks Jerry. I didn't do any testing or deep thinking or even see it
it would work. (If I had to do this and test I would have hit those
problems and come up with those solutions.) I just was presenting a
simple approach. The OP was way over-complicating the problem.
BTW, the other thing to be careful about is what if the field is NULL?
You then want to be sure that you get two successive commas (nothing in
this one).
--
Shelly
|
|
|