First, I’m new in Yii, so if I’m not with the framework standards, please notify me!
I had some problem with pagination and searching for it I found a lot of explanations but none worked well. Messing around I made to solutions:
1 – pagination for simple query
2 – Pagination for complex queries (multiple tables etc)
[i]First, define a new param at config/main.php. Find it at last lines
'params'=>array(
'listPerPage'=> 10, // <-- insert this line with the number you prefer
[/i]
[size="4"]1. Simple way[/size]
CONTROLLER
// just an example to “illustrate” the tutorial, make the proper adaptations
$criteria = new CDbCriteria();
$criteria->condition = 'collumnName1 = :id';
$criteria->order = 'id DESC';
$criteria->params = array (':id'=>$id);
$item_count = ModelNameX::model()->count($criteria);
$pages = new CPagination($item_count);
$pages->setPageSize(Yii::app()->params['listPerPage']);
$pages->applyLimit($criteria); // the trick is here!
$this->render('index',array(
'model'=> ModelNameX::model()->findAll($criteria), // must be the same as $item_count
'item_count'=>$item_count,
'page_size'=>Yii::app()->params['listPerPage'],
'items_count'=>$item_count,
'pages'=>$pages,
));
VIEW
$data = array();
foreach($model as $m){ // loop to get the data (this is different from the complex way)
$data[] = $m->attributes;
}
// the pagination widget with some options to mess
$this->widget('CLinkPager', array(
'currentPage'=>$pages->getCurrentPage(),
'itemCount'=>$item_count,
'pageSize'=>$page_size,
'maxButtonCount'=>5,
//'nextPageLabel'=>'My text >',
'header'=>'',
'htmlOptions'=>array('class'=>'pages'),
));
[size="4"]2. Complex Way[/size]
CONTROLLER
// just an example to “illustrate” the tutorial, make the proper adaptations
$page = (isset($_GET['page']) ? $_GET['page'] : 1); // define the variable to “LIMIT” the query
$query1 = Yii::app()->db->createCommand() //this query contains all the data
->select(array('t2.*'))
->from(array('table1Name t1', 'table2Name t2'))
->where('t1.collumName = '.$id.' AND t2. collumName = t1. collumName and t2. collumName > now()')
->order('t2. collumName DESC')
->limit(Yii::app()->params['listPerPage'], $page-1) // the trick is here!
->queryAll();
$item_count = Yii::app()->db->createCommand() // this query get the total number of items,
->select('count(*) as count')
->from(array('table1Name t1', 'table2Name t2')) // must be the same as $query1 !!!!
->where('t1. collumName = '.$id.' AND t2.id = t1. collumName and t2. collumName > now()') // must be the same as $query1 !!!!
->queryAll(); // do not LIMIT it, this must count all items!
// the pagination itself
$pages = new CPagination($item_count);
$pages->setPageSize(Yii::app()->params['listPerPage']);
// render
$this->render('view',array(
'query1'=>$query1,
'item_count'=>$item_count,
'page_size'=>Yii::app()->params['listPerPage'],
'pages'=>$pages,
));
VIEW
foreach($query1 as $q){} // loop to get data
// the pagination widget with some options to mess
$this->widget('CLinkPager', array(
'currentPage'=>$pages->getCurrentPage(),
'itemCount'=>$item_count,
'pageSize'=>$page_size,
'maxButtonCount'=>5,
//'nextPageLabel'=>'My text >',
'header'=>'',
'htmlOptions'=>array('class'=>'pages'),
));