244863 Posted March 11, 2013 Share Posted March 11, 2013 (edited) <?php class ReportsController extends AppController { public function asset_cat_aov() { $this->set('title_for_layout', __('Asset Category Average Order Value', true)); $this->set('data', $this->Report->query("SELECT * FROM report_asset_cat_aov AS Report;")); } public function asset_aov() { $this->set('title_for_layout', __('Asset Average Order Value', true)); $this->set('data', $this->Report->query("SELECT * FROM report_asset_aov AS Report;")); } public function accrual_export() { ini_set('max_execution_time', 600); Configure::write('debug', 0); if($this->request->is('post')) { $filename = "export_".date("Y.m.d").".csv"; $results = $this->Report->query(" SELECT 'job_id', 'date', 'days', 'type', 'status', 'job_client_reference', 'site_client_reference', 'priority', 'fascia', 'address_line_4', 'postal_code', 'area_name', 'asset_category', 'asset', 'total', 'description' FROM report_accrual AS Report; "); $csv_file = fopen('php://output', 'w'); header('Content-type: application/csv'); header('Content-Disposition: attachment; filename="'.$filename.'"'); $headers = array( 'ID', 'Date', 'Days', 'Type', 'Status', 'Job Ref', 'Site Ref', 'Priority', 'Fascia', 'Address', 'Postal Code', 'Area Name', 'Asset Category', 'Asset', 'Total', 'Description' ); fputcsv($csv_file,$headers,',','"'); // Each iteration of this while loop will be a row in your .csv file where each field corresponds to the heading of the column foreach($results as $result) { $row = array( $result['Report']['id'], $result['Report']['date'], $result['Report']['days'], $result['Report']['type'], $result['Report']['status'], $result['Report']['job_client_reference'], $result['Report']['site_client_reference'], $result['Report']['priority'], $result['Report']['fascia'], $result['Report']['address_line_4'], $result['Report']['postal_code'], $result['Report']['area_name'], $result['Report']['asset_category'], $result['Report']['asset'], $result['Report']['total'], $result['Report']['description'] ); fputcsv($csv_file,$row,',','"'); } fclose($csv_file); } $this->layout = false; $this->render(false); return false; } } ?> http://localhost/index.php/reports/accrual_export this link gives me a blank page, I am expecting it to give me a file download??? Edited March 11, 2013 by ignace Use code tags Quote Link to comment https://forums.phpfreaks.com/topic/275502-export-to-csv/ Share on other sites More sharing options...
ignace Posted March 11, 2013 Share Posted March 11, 2013 (edited) Please use code tags Edited March 11, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/275502-export-to-csv/#findComment-1418020 Share on other sites More sharing options...
244863 Posted June 3, 2013 Author Share Posted June 3, 2013 Hi, Went with this in the end. $this->set('account_status', $this->Report->query("SELECT * FROM report_account_status AS Report;"));$this->layout = 'csv/default'; Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/275502-export-to-csv/#findComment-1433800 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.