Forum Moderators: coopster

Message Too Old, No Replies

Export a table to CSV

         

ClosedGL

12:32 pm on Jul 26, 2005 (gmt 0)

10+ Year Member



I'm using PHP and MySQL to produce a table of figures in a webpage. Nothing special there, but what I would like to do is continue to do this but also provide a button at the bottom of the page marked "Export as CSV". When the user clicks this link I would like all of the data to be automagically pumped into a CSV file and a Save As... dialog prompting the user to appear on the screen.

I realise that the following code will go somewhere to helping, but I'm not sure how to make the button trigger the execution of this code.

$size_in_bytes = strlen($csv_output);
header("Content-disposition: attachment; filename=" .
date("Y-m-d").".csv; size=$size_in_bytes");

Its Tuesday today and I swear my brain thinks it's Monday.

Thanks for your help in advance

dreamcatcher

3:10 pm on Jul 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

Check out the PHP Scripts Repository, there are a few classes there for generating CSV files that work pretty well.

http://www.phpclasses.org [phpclasses.org]

dc

jatar_k

5:23 pm on Jul 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



we have one here
Generating a CSV for Download from MySQL [webmasterworld.com] msg 9

from our PHP LIbrary [webmasterworld.com]

though coopster found a bug when trying to save the file, for me it works perfectly in the scenario you described

coopster

6:58 pm on Jul 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yeah, MS IE chokes on the "attachment" disposition. Actually, it won't if you get prompted and click "Save" but if you click "Open" during the dialog it pukes out. However, change the disposition to "inline" and it will open the pushed content in the browser.

Note, this only happens on data pushed to the browser via echo/print. Files read from the filesystem work like a charm.

jatar_k

7:10 pm on Jul 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



coop just likes breaking all my stuff ;)

coopster

10:05 pm on Jul 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hehe, not at all. I'll figure this one out ... eventually.

coopster

1:21 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Eventually has finally arrived. I'll probably do a better write-up on this, but for those who can't wait, the issue has to do with content negotiation and MSIE's (incorrect) handling of it. You can get all your headers right, you can have it working in every browser, but you throw content negotiation into the mix and MSIE pukes.

The simple resolution? Add a directive to your Apache configuration ...

<Files mydownloadscript.php> 
BrowserMatch MSIE force-no-vary
</Files>

dreamcatcher

2:14 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Coop,

Isn`t there a way round it using 'application/octetstream' or 'application/octet-stream' headers for Opera or IE? I thought I read this somewhere? Maybe PHP.net?

dc

jatar_k

4:24 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



yeah dc, that is supposed to work and the code I linked to has that in it and works flawlessly for me but coop was having an issue with IE trying to be helpful when you would save the file and then open it, just wouldn't work.

hence the long pounding of forehead against keyboard trying to keep IE from screwing things up as usual.

The fix above finally made it be adult about the whole thing and do what it was told. ;)

dreamcatcher

6:07 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks jatar_k. Just think though, if there weren`t all these issues with browsers, it wouldn`t be half as much fun. :)

jatar_k

6:45 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm gettin old, I could use a little less fun ;)

ergophobe

7:27 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




I'm gettin old,

Well, I'm pretty sure I have at least a decade on you and I can say that I've been debugging your posts and I think I've found your problem:


The fix above finally made it be adult about the whole thing and do what it was told. ;)

Adults don't do what they're told. Not unless they want to get old before their time.

jatar_k

7:33 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ClosedGL,

after we drove your topic painfully into another discussion altogether did you get it all figured out?

>> at least a decade

at very least, hehe