Filter and order elements of a page without database access

Hi,

Today I was asking myself a simple question, how to optimize a page that displays Messages for example where you can choose different orders and filters.

So far I was using in my controller something like:




public function actionMyMessages()

{ 

    $criteria = new CDbCriteria;

    -Initilizing my criterias using different $_GET['xxx'] and session variables-

    $messages = Message::model->findAll($criteria);

    $this->render(myMessages,array('messages'=>$messages);

}



And in my View something like :




<script type="text/javascript">

function selectCategory(dropDownList)

{

    var value = dropDownList.options[dropDownList.selectedIndex].value;

    window.location = "index.php?r=user/myMessages&idCategory=" + value;

}

</script>


<?php echo CHtml::dropDownList('category',null,array('1','2'), array('empty'=> 'All','id'=>'category','onChange'=>'selectCategory(this)')); ?>



But everytime I want to add a filter or change the displaying order, then it needs to access the database again, which is stupid because we already have all the informations… so I should do the filtering and ordering without accessing the database again…

I was using ASP.NET before, that was the function of the ViewState… but I am not sure what is the best way to do using PHP and Yii? I guess we could use the session variables…

Any suggestion?