3

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

Charles
  • 50,943
  • 13
  • 104
  • 142
Awais Qarni
  • 17,492
  • 24
  • 75
  • 137

3 Answers3

8

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.

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
6

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

Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
Toni
  • 61
  • 1
  • 1
0

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.

Community
  • 1
  • 1
WoodrowShigeru
  • 1,418
  • 1
  • 18
  • 25