Jump to content

export to csv


244863

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

Edited by ignace
Use code tags
Link to comment
Share on other sites

  • 2 months later...

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,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.