Yii Cgrid View

Hi,

I am new to Yii. I do search for some data under some criteria. This gets render in my grid View, But when I click on 2nd page, all the records from my DB are shown. please help me solving this.

My Action:

public function actionIndex()

{


	$model=new NimsoftAlerts;


            if(isset($_POST['NimsoftAlerts']))


	{


                    $model->attributes=$_POST['NimsoftAlerts'];


                    $params=array(


			'host_start_date'=>$model->host_start_date,


			'host_end_date'=>$model->host_end_date,


                    );


                    if($model->validate())


                    { 


                    $criteria = new CDbCriteria();


		$criteria->condition = "alert_datetime  >= '$model->host_start_date' and alert_datetime <= '$model->host_end_date' and alert_itsm_ack_status IS NULL";


                    $details = NimsoftAlerts::model()->findAll($criteria);


		$dataProvider=new CActiveDataProvider('NimsoftAlerts',array(


				'criteria'   => $criteria,


                        'pagination'=>array(


					'params'=>$params,


			),


			'sort'=>array(


					'params'=>$params,


					'attributes'=>array('host_start_date','host_end_date'),


			),


                        ));


                   }


                   else


                   $dataProvider=new CActiveDataProvider('NimsoftAlerts');


                   


            }


	else


             $dataProvider=new CActiveDataProvider('NimsoftAlerts',array(


                 


                 'pagination'=>array(


					'params'=>$params,


			),


			'sort'=>array(


					'params'=>$params,


					'attributes'=>array('host_start_date','host_end_date'),


			),


                     ));


            


            if($_REQUEST['isXLSDownload']=='1')


	{


                    $xlsName='Missing_Host_Details_'.date('YmdHis').'.xls';


		$sheetName='Missing Host Details';


		$headerTxt='Host Details';


		$arrTh=array(


                                                    'alert_device'=>array('label'=>'Alert Device'),


						'alert_event_time'=>array('label'=>'Alert Event Time'),


						'alert_datetime'=>array('label'=>'Alert Datetime'),


						


						


		);


                    


                    $this->generateCXLS($xlsName,$sheetName,$criteria,$model,$headerTxt,$arrTh);


                    


	}


            


	


	$viewNimsoftTktSts = $model->dispNimsoftTktSts(); 


	// renders the view file 'protected/views/site/index.php'


	// using the default layout 'protected/views/layouts/main.php'


	


            $this->render('index',array(


			'viewNimsoftTktSts'=>$viewNimsoftTktSts,


			'dataProvider'=>$dataProvider,


			'model'=>$model,


	));


           


            


             


}

My View:

<?php

/* @var $this NimsoftAlertsController */

/* @var $dataProvider CActiveDataProvider */

/* $this->breadcrumbs=array(

'Nimsoft Alerts',

); */

/*

$this->menu=array(

array('label'=&gt;'Create NimsoftAlerts', 'url'=&gt;array('create')),


array('label'=&gt;'Manage NimsoftAlerts', 'url'=&gt;array('admin')),

);*/

?><div class="form">

<?php $form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'nimsoft-alerts-form',


// Please note: When you enable ajax validation, make sure the corresponding


// controller action is handling ajax validation correctly.


// There is a call to performAjaxValidation() commented in generated controller code.


// See class documentation of CActiveForm for details on this.


'enableAjaxValidation'=&gt;false,

)); ?>

<h1>Missing Hosts List</h1>

<?php //echo $form->errorSummary($model); ?>

<div style="float:left;">

    &lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'host_start_date'); ?&gt;


	&lt;?php


            Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');


        &#036;this-&gt;widget('CJuiDateTimePicker', array(


        'attribute' =&gt; 'host_start_date',


        'language' =&gt; '',


        'model' =&gt; &#036;model,


        'options' =&gt; array(


        'mode' =&gt; 'focus',


        'dateFormat' =&gt; 'yy-mm-dd',


        //'minDate'=&gt;'0',


        'showAnim' =&gt; 'slideDown',


        ),


        'htmlOptions' =&gt; array(


        'style'=&gt;'height:20px;',


        'value' =&gt; &#036;model-&gt;host_start_date,


        


        ),


        ));


        ?&gt;


      &lt;?php echo &#036;form-&gt;error(&#036;model,'host_start_date'); ?&gt;      


&lt;/div&gt;

</div><div style="float:left;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>

<div style="float:left;">

<div class="row">

	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'host_end_date'); ?&gt;


	&lt;?php


            Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');


        &#036;this-&gt;widget('CJuiDateTimePicker', array(


        'attribute' =&gt; 'host_end_date',


        'language' =&gt; '',


        'model' =&gt; &#036;model,


        'options' =&gt; array(


        'mode' =&gt; 'focus',


        'dateFormat' =&gt; 'yy-mm-dd',


        //'minDate'=&gt;'0',


        'showAnim' =&gt; 'slideDown',


        ),


        'htmlOptions' =&gt; array(


        'style'=&gt;'height:20px;',


        'value' =&gt; &#036;model-&gt;host_end_date,


        


        ),


        ));


        ?&gt;


      &lt;?php echo &#036;form-&gt;error(&#036;model,'host_end_date'); ?&gt;      


&lt;/div&gt;

</div>

<div class="row buttons">

    &lt;?php echo CHtml::button('Search',array('submit' =&gt; array('Site/index')));?&gt;


    &lt;?php echo CHtml::button('Search and Export',array('submit' =&gt; array('Site/Export')));?&gt;

</div>

<?php $this->endWidget(); ?>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<!–<div class="row buttons">

&lt;a href=&quot;&lt;?php //echo &#036;this-&gt;createUrl('Site/Index',array('id'=&gt;&#036;cust_id,'isXLSDownload'=&gt;1));?&gt;&quot; title=&quot;Export For All Customers&quot; class=&quot;btn btn-primary circle_ok&quot; style=&quot;text-decoration: none; color:#FF3333;&quot; &gt;&lt;b&gt;Export All&lt;/b&gt;&lt;/a&gt;

</div>–>

 &lt;?php 

$this->widget(‘zii.widgets.grid.CGridView’, array(

'dataProvider'=&gt;&#036;dataProvider,





'enableSorting' =&gt; false,


'columns'=&gt;array(


                         array(            // display 'create_time' using an expression


			            'name'=&gt;'alert_device',


                                        'value'=&gt;'&#036;data-&gt;alert_device',


			),


                         array(            // display 'create_time' using an expression


			            'name'=&gt;'alert_event_time',


                                        'value'=&gt;'&#036;data-&gt;alert_event_time',


			),


     


                         array(            // display 'create_time' using an expression


			            'name'=&gt;'alert_datetime',


                                        'value'=&gt;'&#036;data-&gt;alert_datetime',


			),


 ),


//'itemView'=&gt;'_view',

));

?>

</div>

Hmm,… checking the code…,


if(isset($_POST['NimsoftAlerts']))

{

    // some codes here...

}

else {

        $dataProvider = new CActiveDataProvider( 'NimsoftAlerts', array(

            'pagination' => array(

                'params' => $params, // $params variable is undefined

            ),

            'sort'   	=> array(

                'params' 	=> $params, // This one too...

                'attributes' => array( 'host_start_date', 'host_end_date' ),

            ),

        ) );

}

You should define a $params variable outside the if condition not only in the if condition… like this one,




$params = 'something here'; // this one


if(isset($_POST['NimsoftAlerts']))

{

    // some codes here...

}

else {

        $dataProvider = new CActiveDataProvider( 'NimsoftAlerts', array(

            'pagination' => array(

                'params' => $params, // $params variable is undefined

            ),

            'sort'   	=> array(

                'params' 	=> $params, // This one too...

                'attributes' => array( 'host_start_date', 'host_end_date' ),

            ),

        ) );

}

change your criteria and utilize params…


if($model->validate())

        {

            $criteria = new CDbCriteria();

            $criteria->condition = "alert_datetime >= :host_start_date and alert_datetime <= :host_end_date and alert_itsm_ack_status IS NULL";

            $criteria->params = array(

                'host_start_date' => $model->host_start_date,

                'host_end_date' => $model->host_end_date,

            );

       	$details = NimsoftAlerts::model()->findAll($criteria); // This is unnecessary; base on your code.

            ...

I replaced all, but same result. In the $params before if condition, I defined as

$params=array(

			'host_start_date'=&gt;&#036;model-&gt;host_start_date,


			'host_end_date'=&gt;&#036;model-&gt;host_end_date,


                    );

is this correct?

At this point, before if(), $model->attributes are set to the default, you haven’t assigned the returned form values to $model yet. By declaring $param at the beginning you are setting the scope of visibility to the whole function, then within various conditionals (if()) you can set it as needed to be used later, outside of the conditionals.