Simple CSV Export in CakePHP from Mysql Database

To export data in the CSV format in the php/ cakephp we can do by using this example. I hope this will be helpful. 


PHP Code :

<?php 

 function download(){
        $fulldata    =    $this->Session->read('csvProductData');    //Data to be exported
        $machine_list = $this->Machine->find('list',array('fields'=>array('Machine.id','Machine.machine_name')));

        if(!empty($fulldata)){
                $csv    =    array();
                $totalRecords = count($fulldata);
                $header_row = array('Product Name','Product Code','Model','Category','Machine','Cost Price','Sale Price','Total Quantity','Used Quantity','Remaining Quantity','Assign Quantity');//Headings in the CSV file.
                $csv[] = implode(",",$header_row);
                if($totalRecords!=0){
                    $filename = 'products_record.csv';
                    foreach($fulldata as $key=>$value){
                        $entries = array();
                        $entries[] = "\"".$value['Product']['product_name']."\"";
                        $entries[] = "\"".$value['Product']['product_code']."\"";
                        $entries[] = "\"".$value['Product']['model_name']."\"";
                        $entries[] = "\"".$value['Category']['name']."\"";
                        $entries[] = "\"".$machine_list[$value['Product']['machine_id']]."\"";
                        $last_key = array_pop(array_keys($value['ProductOption']));
                        $entries[] = "\"".$value['ProductOption'][$last_key]['cost_price']."\"";
                        $entries[] = "\"".$value['Product']['sale_price']."\"";
                        $entries[] = "\"".$value['Product']['total_quantity']."\"";
                        $entries[] = "\"".$value['Product']['used_quantity']."\"";
                        $entries[] = "\"".$value['Product']['remaining_quantity']."\"";
                        $entries[] = "\"".$value['Product']['assign_quantity']."\"";
                        $csv[] = implode(",",$entries);
                    }
                }else{
                    $filename = 'products_record.csv';
                    $entries[] = array("There are no records of products");
                }
                header("Cache-Control: public");
                header("Content-Description: File Transfer");
                header("Content-Disposition: attachment; filename=$filename");
                header("Content-Type: application/csv");
                header("Content-Transfer-Encoding: binary");
                echo implode("\n",$csv);
                die;
        }else{
            $csv = array();
            $filename = 'products_record.csv';
            $entries = array("There are no records Inventory");
            $csv = $entries;
            header("Cache-Control: public");
            header("Content-Description: File Transfer");
            header("Content-Disposition: attachment; filename=$filename");
            header("Content-Type: application/csv");
            header("Content-Transfer-Encoding: binary");
            echo implode("\n",$csv);
            die;
           
           
        }
       
    }

?>

Comments