Export php generated data to CSV

Every so often, I am reminded why I should never become an IT consultant (not considering the fact I am wholly unqualified). I was writing some nifty (for me) php scripts to generate a table of scoring data from a mysql database table. I got everything to output just the way I wanted it but since I’m not the primary end user, I decided to have the end user test it. A couple of minor mods and then I got hit with the “I want to print it out like I used to do with Excel” request.

So I fiddled with the css, stuck the page-break-before styling in, and carried the header through the pagination. Got everything working and looking great when I got hit with the “Does it have to be 4 pages long?” Ugh.

Now if all the data was stored in the database table, I could just dump the table into a csv file and be done with it but that wasn’t the case. The script computed the bulk of the data. So I figured if I could dump the data into a csv file as it was being generated, the end user could just import it into Excel and manipulate/print it as his Excel skills allowed. I found this nice little tutorial which gave me the information I needed to modify my script quickly and most importantly, without trial and error.

WLScripting.com » Export data to CSV Comma Separated Value

3 thoughts on “Export php generated data to CSV

  1. Hi, I am having this issue myself. I have already been to the website that you mention above. However, when I open the csv after download it included the quotation marks and still separates fields at the commas. Strangely enough, this method works for the first field on the left. But the others it doesn’t.

    Any ideas as to why this would be?


  2. Excel can be finicky importing. For instance, if you are importing US zip codes from a csv directly, Excel will chop the leading zero off. If you’ve got a lot of commas in the text you are importing, use another one of the delimiters Excel accepts.

  3. I am glad that you found that tutorial easy to use. If you are interested in seeing anything else added to the tutorial list on WLScripting let me know. Have a great day!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.