No Pagination Using Cgridview

hi gyz, I created a search interface… which comes with pagination… So I did a model, view and then its controller as usual… The view has a Datte range… from first_date to second_date . So the results are returned using a CGridView widget in a grid…and paginates so well The problem is when I click on next pages say page 2…3.4 no data is loaded… I have put the sources below here:

/// View

<?php

?>

<br><br>

<h3> Cancel Bills . </h3>

<br>

<div class="form">

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

'id'=&gt;'bill-search-form',


'enableAjaxValidation'=&gt;false,

)); ?>

&lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;





&lt;?php echo &#036;form-&gt;errorSummary(array(&#036;model)); ?&gt;

<table>

<tr>

<td>

<?php echo $form->labelEx($model,‘first_date’); ?><br>

			&lt;?php

$this->widget(‘zii.widgets.jui.CJuiDatePicker’, array(

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

‘attribute’=>‘first_date’,

‘value’=>(isset(Yii::app()->request->cookies[‘first_date’])) ? Yii::app()->request->cookies[‘first_date’]->value : ‘’,

// additional javascript options for the date picker plugin

‘options’=>array(

   'showAnim'=&gt;'fold',

‘dateFormat’=>‘yy-mm-dd’,

),

‘htmlOptions’=>array(

// ‘style’=>‘height:40px;’,

),

));

?>

<br><?php echo $form->error($model,‘first_date’); ?>

</td>

<td>

<?php echo $form->labelEx($model,‘second_date’); ?><br>

			&lt;?php

$this->widget(‘zii.widgets.jui.CJuiDatePicker’, array(

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

‘attribute’=>‘second_date’,

‘value’=>(isset(Yii::app()->request->cookies[‘second_date’])) ? Yii::app()->request->cookies[‘second_date’]->value : ‘’,

// additional javascript options for the date picker plugin

‘options’=>array(

   'showAnim'=&gt;'fold',

‘dateFormat’=>‘yy-mm-dd’,

),

‘htmlOptions’=>array(

// ‘style’=>‘height:40px;’,

),

));

?>

<br><?php echo $form->error($model,‘second_date’); ?>

</td>

</tr>

<tr>

<td>

<?php echo $form->labelEx($model,‘cust_contnum’); ?><br>

<?php echo $form->textField($model,‘cust_contnum’,array(‘value’=>(isset(Yii::app()->request->cookies[‘cust_contnum’])) ? Yii::app()->request->cookies[‘cust_contnum’]->value : ‘’,)); ?>

<br><?php echo $form->error($model,‘cust_contnum’); ?>

</td>

<td>

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


	&lt;?php echo CHtml::submitButton('Submit To Search'); ?&gt;


&lt;/div&gt;

</td>

</tr></table>

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

</div>

/// Model

<?php

class Cancelpay extends CActiveRecord

{

public  &#036;first_date;


public  &#036;second_date;


public  &#036;cust_contnum;


public static function model(&#036;className=__CLASS__)


{


	return parent::model(&#036;className);


}





public function tableName()


{


	return 'cancelpay';


}





 public function primaryKey(){


   return 'BillID';


    }





public function rules()


{





	return array(


	     


		array('first_date, second_date', 'required'),


		array('cust_contnum', 'safe'),


		array('first_date,second_date', 'date', 'format'=&gt;'yyyy-MM-dd'),


		array('cust_contnum', 'numerical', 'integerOnly'=&gt;true),			


		array('first_date,second_date', 'compare', 'compareValue'=&gt;date('Y-m-d').'23:59:59', 'operator'=&gt;'&lt;','message'=&gt;'No Date should be greater than today'),


	    array('first_date', 'compare', 'compareAttribute'=&gt;'second_date', 'operator'=&gt;'&lt;=','message'=&gt;'First Date Cannot be Greater than Second Date'),


		array('cust_contnum', 'authAccountnum','allowEmpty'=&gt;true),


		array('TotalBill', 'numerical'),


		array('BillingDate', 'safe'),





		array('BillID, FirstName, ContractNo, BillingDate, TotalBill,first_date, second_date,cust_contnum', 'safe', 'on'=&gt;'search'),


	);


}








public function relations()


{





	return array(


	);


}


 public function authAccountnum(&#036;attribute,&#036;params)


    {


    &#036;record=Custdetails::model()-&gt;findByAttributes(array('ContractNo'=&gt;&#036;this-&gt;cust_contnum));


    if(&#33;empty(&#036;this-&gt;cust_contnum)){


    if(&#036;record===null){


    &#036;this-&gt;addError(&#036;attribute, 'Account Number for the Customer You Entered does not exist');


    } }


  }





public function attributeLabels()


{


	return array(


		'BillID' =&gt; 'Bill ID',


		'FirstName' =&gt; 'Customer Name',


		'ContractNo' =&gt; 'Account No',


		'BillingDate' =&gt; 'Billing Date',


		'TotalBill' =&gt; 'Amount Billed',


	);


}








public function search()


{





	&#036;criteria=new CDbCriteria;





        &#036;criteria-&gt;condition = &quot;BillingDate  &gt;= '&#036;this-&gt;first_date' AND  BillingDate &lt;= '&#036;this-&gt;second_date'&quot;;


		&#036;criteria-&gt;compare('ContractNo',&#036;this-&gt;cust_contnum,true );











	return new CActiveDataProvider(&#036;this, array(





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


		'pagination' =&gt; false,


	));


}

}

/// Controllers Action

public function actionIndex()


{


	unset(Yii::app()-&gt;request-&gt;cookies['first_date']);  // first unset cookie for dates


    unset(Yii::app()-&gt;request-&gt;cookies['second_date']);


	unset(Yii::app()-&gt;request-&gt;cookies['cust_contnum']);


  //  &#036;model=new Testing;  


	&#036;model=new Cancelpay('search');


	&#036;model-&gt;unsetAttributes();  // clear any default values


	   if(isset(&#036;_POST['Cancelpay']))


        {


		&#036;model-&gt;attributes=&#036;_POST['Cancelpay'];


		if(&#036;model-&gt;validate()) 


		{


		


		&#036;model-&gt;attributes=&#036;_POST['Cancelpay'];


		


        Yii::app()-&gt;request-&gt;cookies['first_date'] = new CHttpCookie('first_date', &#036;model-&gt;first_date);  // define cookie for first_date


        Yii::app()-&gt;request-&gt;cookies['second_date'] = new CHttpCookie('second_date', &#036;model-&gt;second_date);


		Yii::app()-&gt;request-&gt;cookies['cust_contnum'] = new CHttpCookie('cust_contnum', &#036;model-&gt;cust_contnum);


   /*     &#036;this-&gt;ffirst_date = &#036;model-&gt;first_date;


        &#036;this-&gt;ssecond_date = &#036;model-&gt;second_date.' 23:59:59';


		&#036;this-&gt;ccust_contnum = &#036;model-&gt;cust_contnum; */


		&#036;this-&gt;render('admin',array(


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


         ));


		 Yii::app()-&gt;end();


		 }


		 else  { &#036;this-&gt;render('index',array('model'=&gt;&#036;model));    Yii::app()-&gt;end();}	


       }


	 


            &#036;this-&gt;render('index',array('model'=&gt;&#036;model)); 


} 








Any help I will appreciate, thankyou

Hi Donatus,

Welcome to the forum.

The pagination of CGridView works through ‘GET’ method, not ‘POST’. And it will be handled by the gridview’s data provider using the CPagination instance … but you are setting false to ‘pagination’ property.

I would start using a gii-generated admin page as the skeleton … it has all you need.

http://www.yiiframework.com/wiki/381/cgridview-clistview-and-cactivedataprovider/

[PS]

About the posting, I’d like to ask you two things:

  1. Use "CODE" tag ("<>" button) to make your code much more easy to read.

  2. Drop the irrelevant parts of the code.

Otherwise you will lose many potential readers. :)

Thank you so much softark: but still in vain Though this time I tried this time:

Controller Action:




	public function actionIndex()

	{

	 unset(Yii::app()->request->cookies['first_date']);  // first unset cookie for dates

     unset(Yii::app()->request->cookies['second_date']);

	 unset(Yii::app()->request->cookies['cust_contnum']);

        $model=new Testing;

		$model->unsetAttributes();  // clear any default values

		$this->classess=new Cancelpay;




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

	    {       

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

				if($model->validate()) {

				Yii::app()->request->cookies['first_date'] = new CHttpCookie('first_date', $model->first_date);

				Yii::app()->request->cookies['second_date'] = new CHttpCookie('second_date', $model->second_date);

				Yii::app()->request->cookies['cust_contnum'] = new CHttpCookie('cust_contnum', $model->cust_contnum);

				$model->second_date=$model->second_date.' 23:59:59';

				$this->conttnum = $model->cust_contnum;

		        $criteria=new CDbCriteria;

                $criteria->addBetweenCondition('BillingDate', ''.$model->first_date.'', ''.$model->second_date.'');

				$criteria->compare('ContractNo',$this->conttnum,true);

				$count=Cancelpay::model()->count($criteria);

                $pages=new CPagination($count);

                $pages->pageSize=10;

                $pages->applyLimit($criteria);

                $this->dtprovider = new CActiveDataProvider(get_class($this->classess), array(

                    'criteria'=>$criteria,

                ));

              //  $this->redirect(array('admin',array('dtprovider'=>$this->dtprovider)));    

 

               $this->render('admin',array('dataProvider'=>$this->dtprovider ,'pages' =>$pages));		

              Yii::app()->end();  }


        else  { $this->render('index',array('model'=>$model));    Yii::app()->end();}		  

	    }

		else{

	            $this->render('index',array('model'=>$model)); }

	} 






The Class Testing has the date range fields and the other class Cancellpay has the table definition of the data to be searched. Any further help will appreciate.

View:




<div class="form">

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

	'id'=>'bill-search-form',

	'enableAjaxValidation'=>false,

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


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

<table>

<tr>

<td>

 <?php echo $form->labelEx($model,'first_date'); ?><br>

 				<?php

$this->widget('zii.widgets.jui.CJuiDatePicker', array(

    'model'=>$model,

   'attribute'=>'first_date',

   'value'=>(isset(Yii::app()->request->cookies['first_date'])) ? Yii::app()->request->cookies['first_date']->value : '',

   // additional javascript options for the date picker plugin

   'options'=>array(

       'showAnim'=>'fold',

'dateFormat'=>'yy-mm-dd',

   ),

   'htmlOptions'=>array(

   //    'style'=>'height:40px;',

   ),

));

?>


 <br><?php echo $form->error($model,'first_date'); ?>

</td>

<td>

 <?php echo $form->labelEx($model,'second_date'); ?><br>

  				<?php

$this->widget('zii.widgets.jui.CJuiDatePicker', array(

    'model'=>$model,

   'attribute'=>'second_date',

   'value'=>(isset(Yii::app()->request->cookies['second_date'])) ? Yii::app()->request->cookies['second_date']->value : '',

 

   'options'=>array(

       'showAnim'=>'fold',

'dateFormat'=>'yy-mm-dd',

   ),

   'htmlOptions'=>array(

 

   ),

));

?>

 

 <br><?php echo $form->error($model,'second_date'); ?>

</td>

</tr>

<tr>

<td>

 <?php echo $form->labelEx($model,'cust_contnum'); ?><br>

 <?php echo $form->textField($model,'cust_contnum',array('value'=>(isset(Yii::app()->request->cookies['cust_contnum'])) ? Yii::app()->request->cookies['cust_contnum']->value : '',)); ?>

  <br><?php echo $form->error($model,'cust_contnum'); ?>

</td>

<td>

	<div class="row buttons">

		<?php echo CHtml::submitButton('Submit To Search'); ?>

	</div>

</td>

</tr></table>

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

</div>






I’m not sure which model you are showing in the grid view, Testing or Cancelpay. … Maybe Cancelpay, I guess.

Anyway, the gii-generated admin page has almost all the things that you will need.

  1. You can use the "advanced search" form as the starting block for your customized search form.

You may want to add ‘first_date’ and ‘second_date’ in it using date pickers.

  1. Modify the “search” method of the model and use ‘first_date’ and ‘second_date’ in it.

Then, the admin page should work as you expect.

[PS]

Would you please re-edit your first post using CODE tag?

I found it at last, I just used the advanced search form and changed the attributes to suit my search criteria, now when I load the admin.php page no results are displayed. However when I type and search, I now use the default yii pagination. I just realized I was re-engineering the wheel… thank you so much softark.