Criteria reset on next page

I am sure this is a simple issue but I couldn’t find a solution on google.

I have a CGridView and a form with search fields to filter the results. The problem is that those filter values don’t hold between pages, so when I go to the next page of results my search values are reset.

Here is a basic example of one of my search functions:


    public function search()

    {

            $criteria=new CDbCriteria();

            if($this->user_id != 0){

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

            }

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

            $criteria->order = 'time desc';

            return new CActiveDataProvider('ActionLog', array(

                    'criteria'=>$criteria,

                    'pagination' => array('pageSize' => 20)

            ));

    }

Is there an easy way to fix this?

Cheers, Tim.

Can anyone help me with this? It has been almost 24 hours :)

Check this extension: http://www.yiiframework.com/extension/remember-filters-gridview

Hi Tim,

  I give an example. Hope it can be helpful to you.



array(

        'name'=>'city_id',

        'filter'=>CHtml::dropDownList('Location[city_id]',isset($_GET['Location']['city_id']) ? (int)$_GET['Location']['city_id'] : 0 ,City::model()->findAll() ,array('empty'=>trans('Select city'))),

	'value'=>'$data->city->name',

       ),




Okay, I worked it out. Search values have to be submitted by GET, not POST and they will be saved between pages. Does anyone know how to get it working using POST? It would be nice, otherwise GET will do.

Using POST over GET may look cleaner but it does prevent you bookmarking pages. Just a thought before you plough ahead and fix it.

I realize that but they are search results so bookmarking the page isn’t really necessarily, I would rather the URL wasn’t confusing to the user instead. If modifying it to use POST requires a lot of effort though then using GET will be sufficient, and I can call the ability to bookmark result sets a feature :P

With GET they are saved because you are sending the searched values in the URL… with POST you have the searched values only in the current request POST data… so on next page… you cannot access those values…

One solution is to store them in the session… check the source of the extension I posted you above… it uses sessions for this…