Tuesday, December 16, 2014

create CSV report in DB data YII

Standard

hi guys this is my testing code about yii  CSV report i created data table name as deal  and i created Deal control & crud

this is my data table



CREATE TABLE IF NOT EXISTS `deals` (
  `DealId` int(11) NOT NULL AUTO_INCREMENT,
  `DealName` varchar(255) NOT NULL,
  `Dealdescription` text NOT NULL,
  `Price` int(11) NOT NULL,
  `Discount` float NOT NULL,
  `StartingDate` varchar(50) NOT NULL,
  `EndingDate` varchar(50) NOT NULL,
  `DealCategory` varchar(45) NOT NULL,
  `DriverId` int(11) NOT NULL,
  `CreateDate` datetime NOT NULL,
  `ModifiedDate` datetime NOT NULL,
  `Status` enum('0','1') NOT NULL DEFAULT '0',
  PRIMARY KEY (`DealId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

this is my report funtion in deal controller


condition = "DealId=".$DealId;
                                    $models=  Deals::model()->findAll($criteria);
                                   // echo '
'.  print_r($models,1).'
'; // die(); // Pick a filename and destination directory for the file // Remember that the folder where you want to write the file has to be writable $filename =Yii::getPathOfAlias('webroot').'/report/'.$DealId.'_report_by_'.$DealId.".csv"; // Actually create the file // The w+ parameter will wipe out and overwrite any existing file with the same name $handle = fopen($filename, 'w+'); // Write the spreadsheet column titles / labels fputcsv($handle, array( 'Dea lId', 'Deal Name', 'Deal description', 'Price', 'Discount', 'Start Date', 'EndDate', 'Deal Category', 'Driver Name', 'Status', )); foreach($models as $row) { fputcsv($handle, array ( $row['DealId'], $row['DealName'], $row['Dealdescription'], $row['Price'], $row['Discount'], $row['StartingDate'], $row['EndingDate'], $row['DealCategory'], $row['DriverId'], $row['Status'], )); } // Finish writing the file fclose($handle); $this->redirect(Yii::app()->getBaseUrl(true).'/report/'.$DealId.'_report_by_'.$DealId.'.csv', array('target'=>'_blank')); } $this->render('report',array( 'report'=>$report, )); } }
then i add report.php in view deal folder
add following code in report.php
actionReport(1);

?>
now you can get csv file

thank

2 comments:

  1. Hi,

    I'm using your reportsmcs module, I can't download the csv file, there is no action once I hit the Generate Report button http://prntscr.com/7g0n5u Please help

    ReplyDelete
    Replies
    1. can i know your YII version .this only work eith YII 1.+ not yii 2

      Delete