简体   繁体   中英

Export table data to CSV with PHP, Zend

I have MySql data base and web site, in the web i show database values in table. I made that CSV exports the specify database model(table).

  `$this->view->table = $model->info('name');

    $is_csv = $this->_getParam('csv');

    if ($this->_request->isPost() && $is_csv) {

        $file1 = 'Baudų sąrašas '.$date_from.' iki '.$date_to.'.csv';
        $fp = fopen('php://output', 'w+');
        if ($date_from) {
            $select->where("date >= ?", $date_from . ' 00:00:00');
        }
        if ($date_to) {
            $select->where("date <= ?", $date_to . ' 23:59:59');
        }
            $data = $model->fetchAll($select);
        foreach ($data as $fields) {

            fputcsv($fp, $fields->toArray());
        }

        fclose($fp);

        if ($file1 !== false) {

            header('Content-type: text/plain');
            header('Content-Disposition: attachment; filename="' . $file1 . '"');
            exit;
        }
    }`

And the HTML that calls it:

       form method="post">
            <div class="control-group pull-right">

            <input type="hidden" name="table" value="<?php echo $this->table ?>" />
            <button type="submit" class="btn" name="csv" value="csv"><?php echo Core_Locale::translate('CSV')?></button>
                </div>
        </form>

Now i want to take not from data base, but from function wich has Join's to the database, but when i use return $this->getAdapter()->fetchAll($select); it gives error non object and when i write return $this->fetchAll($select); it gives me as data base data as from function... Do any may have some clue how to do this?

This is not the exact answer for your question, but why don't you try this..

SELECT * FROM my_table INTO OUTFILE 'my_table.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n';

Copied From : export mysql list of tables from mysql to csv file using php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM