473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Output as csv

I'm trying to output the results of a database query as a csv file

The DB output is in $csv

foreach($csv as $array){
$line[]=implode($delimited_by,$array)."\n";
}
header("Content-Type: text/plain");
header("Content-Disposition: inline");

foreach($line as $output){
print $output;
On clicking the link to this file the results are output to the screen.
I can right click and save the output but I'm prompted to save it as a
..php file. How can I make it output as the right kind ( .csv) of file?

--
Regards,

Geoff Berrow
Mar 12 '06 #1
9 21939
Geoff Berrow wrote:
I'm trying to output the results of a database query as a csv file

The DB output is in $csv

foreach($csv as $array){
$line[]=implode($delimited_by,$array)."\n";
}
header("Content-Type: text/plain");
header("Content-Disposition: inline");

foreach($line as $output){
print $output;
On clicking the link to this file the results are output to the screen.
I can right click and save the output but I'm prompted to save it as a
.php file. How can I make it output as the right kind ( .csv) of file?


There is a rfc stating "text/csv" as the mime-type, but it is quite
recent, so probably not widely recognized.
http://www.rfc-archive.org/getrfc.php?rfc=4180

Presently, I think the common way is to set disposition to attachment
and supply a filename, and let the OS figure out how to open it.
header('Content-Disposition: attachment; filename="foo.csv"');
/Bent
Mar 12 '06 #2
Message-ID: <44***********************@dread15.news.tele.dk> from Bent
Stigsen contained the following:
There is a rfc stating "text/csv" as the mime-type, but it is quite
recent, so probably not widely recognized.
http://www.rfc-archive.org/getrfc.php?rfc=4180

Presently, I think the common way is to set disposition to attachment
and supply a filename, and let the OS figure out how to open it.
header('Content-Disposition: attachment; filename="foo.csv"');


Thanks.

I have found that this gives an option to open or dave the file,
however, opening it does not work.

But the following seems to work fine.

header("Content-Type: text/csv");
header("Content-Disposition: inline; filename=$_SESSION[table].csv ");

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 12 '06 #3
Message-ID: <a7********************************@4ax.com> from Geoff
Berrow contained the following:
header("Content-Type: text/csv");
header("Content-Disposition: inline; filename=$_SESSION[table].csv ");


OK, I'm exporting CSV files nicely now but I wondered how other people
tackle the problem of the content containing quotes.

I'm exporting the data like this

"some data", "some more data", "yet more data"

Eventually the file will be imported into Access.

It occurred to me that if the data contained a double quote mark, then
the file could be messed up.

I've done a string replace and changed double quotes to single, but this
doesn't feel like a very elegant or satisfactory solution so I wondered
what other people do?

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 13 '06 #4
NC
Geoff Berrow wrote:

I'm exporting CSV files nicely now but I wondered how other
people tackle the problem of the content containing quotes.


RFC 4180 specifies escaping a double quote by putting another double
quote in front of it. However, RFC 4180's status is "Informational",
and it notes the existence of "considerable differences among
implementations". So you should consult the manual(s) for the software
that you expect to read the CSV you are generating and see what they
suggest.

Cheers,
NC

Mar 13 '06 #5
Message-ID: <11*********************@j33g2000cwa.googlegroups. com> from
NC contained the following:

RFC 4180 specifies escaping a double quote by putting another double
quote in front of it. However, RFC 4180's status is "Informational",
and it notes the existence of "considerable differences among
implementations". So you should consult the manual(s) for the software
that you expect to read the CSV you are generating and see what they
suggest.

Thanks, I'll give it a try.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 13 '06 #6
NC wrote:
RFC 4180 specifies escaping a double quote by putting another double
quote in front of it. However, RFC 4180's status is "Informational",
and it notes the existence of "considerable differences among
implementations".


The OP mentioned importing into Access. Access does indeed support this
method of escaping.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Mar 13 '06 #7
If you've got access to PHP 5.1+, you can just use the already
RFC-compliant fputcsv() function:

http://us3.php.net/fputcsv

Mar 13 '06 #8
um, rfc where? there's no stinkin rfc...

ad**********@gmail.com wrote:
If you've got access to PHP 5.1+, you can just use the already
RFC-compliant fputcsv() function:

http://us3.php.net/fputcsv


Mar 13 '06 #9
ignore me. wasn't paying attention to the thread... interesting.

shriop wrote:
um, rfc where? there's no stinkin rfc...

ad**********@gmail.com wrote:
If you've got access to PHP 5.1+, you can just use the already
RFC-compliant fputcsv() function:

http://us3.php.net/fputcsv


Mar 13 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: -------------------------------------...
3
by: edgekaos | last post by:
Is method 2 valid? Method 1: wstring input = L"STRING"; wstring output = input; transform(output.begin(), output.end(), output.begin(), towupper); Method 2: wstring input = L"STRING";...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
24
by: kalamantina | last post by:
#include "stdafx.h" #include <stdio.h> #define output( x ) printf( #x "\r\n" );fflush( stdout ) class CMyBase { public: CMyBase() { output( CMyBase() ); f(*this);
0
by: newbie | last post by:
i'm a newbie of c language. can anyone help me to implement the code so that I can get the ciphertext from the output. thanks. #ifndef _3DES_H #define _3DES_H #ifndef uint8 #define uint8 ...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
3
by: MatsL | last post by:
Hi, This is seriously driving me crazy, could anyone explain to me why neither of these doesn't produce XHTML compliant output (it is being called in Render() btw): output.WriteLine("<img...
3
by: undshan | last post by:
I am writing a code that needs to open a file, create an output file, run through my function, prints the results to the output file, and closes them within a loop. Here is my code: #include...
5
by: amit.uttam | last post by:
Hey everyone, I've recently jumped big time into python and I'm working on a software program for testing automation. I had a question about proper logging of output. What I would like is: 1....
2
by: gabosom | last post by:
Hi! I've been breaking my head trying to get the output variables from my Stored Procedure. This is my SP code CREATE PROCEDURE GetKitchenOrderDetail( @idService int, --outPut Variables ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.