Jump to content

Archived

This topic is now archived and is closed to further replies.

244863

export to csv

Recommended Posts

 

<?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???

Share this post


Link to post
Share on other sites

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,

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.