Awais Qarni
Awais Qarni

Reputation: 18006

exporting csv in zend framework

I know how to export a csv file in php. Now I want to do this task in zendframe work.

1 I want to know is there any built in functionality that zend provides for this purposes or we have to write the same code that we use in simple php?

2 If I have to use same code then where should I put that whether in model or controller?

3 I have a view where user can select start date, last date and user andthen press enter. On the basis of that selection I want to exprot csv file.

Any one guide me how to accomplish this task

Upvotes: 3

Views: 11593

Answers (3)

WoodrowShigeru
WoodrowShigeru

Reputation: 1594

Building on regilero's answer for this question, I did come up with a Zend solution. Just use a complete query on the database itself:

$result = $this->_db->query(
     'SELECT * FROM `table_xy`'
    .'INTO OUTFILE \'tmp/outputfile.csv\''
    .'FIELDS TERMINATED BY \';\''
    .'ENCLOSED BY \'"\''
    .'LINES TERMINATED BY \'\\n\''
);

Just don't forget to set up the necessary privileges for that user.

Upvotes: 0

Toni
Toni

Reputation: 61

Here is a Zend Framework Action Helper - may be helpful. Note the site and code comments are in German. http://blog.abmeier.de/zend-csv-action-helper

Upvotes: 6

David Snabel-Caunt
David Snabel-Caunt

Reputation: 58361

To address your points in order

  1. There is not a component in the Zend Framework that will generate and dispose of CSV files.

  2. Your controller should just collect request parameters (i.e. date ranges from point 3) and initiate a response. I would create another class which can generate a CSV and then use this Controller Action Helper, SendFile to actually send the CSV file as a download.

  3. Have your controller receive the request parameters using a form. Generate a CSV using your class (point 2) and then use the helper to send the response.

For generating CSV files it's recommended to use fputcsv rather than building strings yourself.

Upvotes: 8

Related Questions