How to Create Pdf Report / Excel Report By Using Parameters (From - Until)

You are viewing revision #5 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.

« previous (#4)

  1. Please create new function in your models or you can look at below:
  2. In your model | e.g. : models/Items.php
  3. And than you have to add this code in action index.
  4. In your controller | e.g. : controller/ItemsController.php
  5. and now in your view/index.php you can add the code or you can look the code below:
  6. In your view | e.g. : view/items/index.php
  7. remain in view but you have to create new php files and use this code:
  8. In your view | e.g. : view/items/view_item_report.php

Asslamualaikum Wr.Wb Hello I'm Fahmi Azain i will share wiki tutorial about Creating Pdf Report / Excel Report by using Parms / Parameters.

First you should follow the requirements below:

For Pdf Report : install pdf report extension in your yii application. and you must to understand to create that report.

For Excel Report : install excel report extension in your yii application. or you can use the default report yii.

after finishing about those requirements now you can read this wiki.

Okay Let's begin

Please create new function in your models or you can look at below:

In your model | e.g. : models/Items.php

public function getMyreport()
        {
            $from=$_REQUEST['from'];
            $until=$_REQUEST['until']; 
		$sql="SELECT * FROM items where CREATED_DATE >= '$from' and CREATED_DATE <= '$until' order by ITEM_ID desc "; // your sql here
		$dataReportItem=new CSqlDataProvider($sql,array(
			'keyField' => 'ITEM_ID',
			'pagination'=>array(
				'pageSize'=>10,
			),
		));	
		return $dataReportItem;
        }

And than you have to add this code in action index.

In your controller | e.g. : controller/ItemsController.php

public function actionIndex(){
if (!Yii::app()->user->isGuest){

                $dataReportItem=Items::model()->getMyreport();
                if(isset($_REQUEST['ExcelReport'])){
                        $from=$_REQUEST['from'];
                        $until=$_REQUEST['until'];
                       Yii::app()->request->sendFile('items_periode_'.$from.'-'.$until.'.xls',
                       $this->renderPartial('view_item_report',array(
			'dataReportItem' =>$ dataReportItem,
                       )),true);                
                }

                if(isset($_REQUEST['PdfReport'])){
                        $from=$_REQUEST['from'];
                        $until=$_REQUEST['until'];
                        $mPDF1 = Yii::app()->ePdf->mpdf();
                        $mPDF1->WriteHTML($this->renderPartial('view_item_report',array(
			'dataReportKas'=>$dataReportKas,
                        ), true));
                        $mPDF1->Output('items_periode_'.$from.'-'.$until,"I");
                
                }
}

and now in your view/index.php you can add the code or you can look the code below:

In your view | e.g. : view/items/index.php

<form method="post">
            <table>
                <tr>
                    <td>Date From</td>
                    <td>Date To</td>
                </tr>
                <tr>
                    <td><input type="date" name="from" value="<?=isset($_GET['from']) ? CHtml::encode($_GET['from']) : '' ; ?>" /></td>
                    <td><input type="date" name="until" value="<?=isset($_GET['until']) ? CHtml::encode($_GET['until']) : '' ; ?>" /></td>
                </tr>
            </table>
        <input type="submit" value="Report to Excel" name="'ExcelReport'">
        <input type="submit" value="Report to Pdf" name="'PdfReport'">
        </form>

remain in view but you have to create new php files and use this code:

In your view | e.g. : view/items/view_item_report.php

<h3>Items | Periode <?php echo $_REQUEST['from']." - ".$_REQUEST['until'];?></h3>
<?php
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
	'fixedHeader' => true,
	'headerOffset' => 40,
	'type' => 'striped',
	'dataProvider' => $dataReportItem,
	'responsiveTable' => true,
	'template' => "{items}",
        'columns'=>array(
            'ID',
            'ITEM_CODE',
            'ITEM_NAME',
            'ITEM_DESC',            
            'CREATED_DATE', // This is a reference for parms / parameters
        ),
	));?>
<div align="left">
    <b>Printed By : <? echo Yii::app()->user->name;?><br/>
Printed At : <? echo date("d/m/Y H:i:s");?></b>
            <div align="right">Copyright &COPY; <?php echo date('Y'); ?> By Jsource</div>
</div>

Okay It's Done! Don't Forget to Say Alhamdulillah for finishing your problems. i hope it works in your apllication

Thanks For Reading

Terima Kasih!

more tutorials visit my blog : Fahmi Azain Blog 1 or Fahmi Azain Blog 2

0 2
7 followers
Viewed: 43 010 times
Version: Unknown (update)
Category: How-tos
Written by: ruderbytes
Last updated by: ruderbytes
Created on: Jan 1, 2014
Last updated: 10 years ago
Update Article

Revisions

View all history

Related Articles