Pagination / refine search results
#1
Posted 02 November 2009 - 05:23 AM
1) Dropdown options to let the user refine the search results, e.g: "Show 10 results per page", "Show 20 results per page", etc.
2) Dropdown options to select "highest to lowest" and "lowest to highest".
3) Indicator to indicate how many results in total and what results are being displayed, e.g: "Displaying results 1-10 of 500".
I'm using CPagination and also CLinkPager, but can't seem to figure out how to do these 3 things. Can anyone help?
#2
Posted 02 November 2009 - 06:56 AM
GSTAR, on 02 November 2009 - 05:23 AM, said:
1) Dropdown options to let the user refine the search results, e.g: "Show 10 results per page", "Show 20 results per page", etc.
2) Dropdown options to select "highest to lowest" and "lowest to highest".
3) Indicator to indicate how many results in total and what results are being displayed, e.g: "Displaying results 1-10 of 500".
I'm using CPagination and also CLinkPager, but can't seem to figure out how to do these 3 things. Can anyone help?
1) the command crud generate in controller a constant "PAGE_SIZE", replace your use in
$pages->pageSize=self::PAGE_SIZE;
by
$pages->pageSize=$_GET['pageSize'] /*pageSize is a "Dropdown options" in form*/
2) use CSort. and send the GET parameter sort="fieldName" or sort="fieldName".desc
(sort is not a filed in form, CSort) see view admin generate for crud
3) use the GET parameter "page" generate for CPagination (if null then page=1)
pageSize (step1)
and "total count"
$init=(($page-1)* $pageSize )+1
$end=$init + $pageSize
'Displaying results '.$init.'-'.$end.' of '.$total_count
sorry for my english
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
#3
Posted 02 November 2009 - 08:09 AM
Thanks. For number 3, can you advise where $page, $pageSize and $total_count are declared?
#4
Posted 02 November 2009 - 08:50 AM
GSTAR, on 02 November 2009 - 08:09 AM, said:
Thanks. For number 3, can you advise where $page, $pageSize and $total_count are declared?
in the controller action
if (!isset($_GET['page'])) $page=1; else $page=$_GET['page'];
$page_size is a field in the form
you can omit this using
$pages->pageSize=self::PAGE_SIZE;
by default in all controler generated by crud
and
$total_count = "TableName"::model()->count($criteria) /* see a controller generated by crud for example*/
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
#5
Posted 02 November 2009 - 08:58 AM
#6
Posted 02 November 2009 - 09:09 AM
public function actionResults()
{
if (!isset($_GET['page']))
{
$page=1;
}
else
{
$page=$_GET['page'];
}
$criteria=new CDbCriteria;
$pages=new CPagination(Property::model()->count($criteria));
//$pages->pageSize=self::PAGE_SIZE;
$pages->pageSize=$_POST[pageSize];
$pages->applyLimit($criteria);
$init=(($page-1)* $pageSize )+1;
$end=$init + $pageSize;
$total_count=Property::model()->count($criteria);
$sort=new CSort('Property');
$sort->applyOrder($criteria);
$models=Property::model()->findAll($criteria);
$this->render('results',array(
'models'=>$models,
'pages'=>$pages,
'sort'=>$sort,
'init'=>$init,
'end'=>$end,
'total_count'=>$total_count,
));
}
#7
Posted 02 November 2009 - 10:07 AM
GSTAR, on 02 November 2009 - 09:09 AM, said:
public function actionResults()
{
if (!isset($_GET['page']))
{
$page=1;
}
else
{
$page=$_GET['page'];
}
$criteria=new CDbCriteria;
$total_count=Property::model()->count($criteria);
$pages=new CPagination($total_count);
//$pages->pageSize=self::PAGE_SIZE;
$pageSize=$_POST['pageSize']
$pages->pageSize=$pageSize;
$pages->applyLimit($criteria);
$init=(($page-1)* $pageSize )+1;
$end=$init + $pageSize;
$sort=new CSort('Property');
$sort->applyOrder($criteria);
$models=Property::model()->findAll($criteria);
$this->render('results',array(
'models'=>$models,
'pages'=>$pages,
'sort'=>$sort,
'init'=>$init,
'end'=>$end,
'total_count'=>$total_count,
));
}
error typo, try again with code modify
the error
$pages->pageSize=$_POST[pageSize]; $pages->pageSize=$_POST['pageSize'];
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
#8
Posted 03 November 2009 - 03:31 AM
#9
Posted 03 November 2009 - 02:41 PM
GSTAR, on 03 November 2009 - 03:31 AM, said:
$_POST['pageSize'] is NULL
try isset($_POST['pageSize'])
or
empty($_POST['pageSize'])
or
"view source code of page" (with browser)and check the id of field "pageSize"
or
put the code of view here
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
#10
Posted 03 November 2009 - 02:42 PM
GSTAR, on 03 November 2009 - 03:31 AM, said:
$_POST['pageSize'] is NULL
try isset($_POST['pageSize'])
or
empty($_POST['pageSize'])
or
"view source code of page" (with browser)and check the id of field "pageSize"
or
put the code of view here
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
#11
Posted 03 November 2009 - 02:43 PM
GSTAR, on 03 November 2009 - 03:31 AM, said:
$_POST['pageSize'] is NULL ?
try isset($_POST['pageSize'])
or
empty($_POST['pageSize'])
or
"view source code of page" (with browser)and check the id of field "pageSize"
or
put the code of view here
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
#12
Posted 04 November 2009 - 10:26 PM
because $_POST['...'] don't send when I click to page-2.
sorry my english is very poor
#13
Posted 05 November 2009 - 05:00 AM
ngocnq, on 04 November 2009 - 10:26 PM, said:
because $_POST['...'] don't send when I click to page-2.
sorry my english is very poor
see http://www.yiiframew...os-de-busqueda/
if(Yii::app()->request->isPostRequest) {
$redirectParams = array();
if (isset($_POST['pageSize'])){
$redirectParams['pageSize'] = $_POST['pageSize'];
}
$this->redirect(array_merge(array('YOUR_ACTION'), $redirectParams));
}
....
and use GET
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
#14
Posted 05 November 2009 - 07:36 AM
Horacio Segura, on 03 November 2009 - 02:43 PM, said:
try isset($_POST['pageSize'])
or
empty($_POST['pageSize'])
or
"view source code of page" (with browser)and check the id of field "pageSize"
or
put the code of view here
No this also happens on first load of the page (when there is no $_GET['page'] variable set). It just says "Displaying results 1-1 of 500".
The code of the view:
<p>Displaying Properties <?php echo $init.'-'.$end.' of '.$total_count; ?></p>
#15
Posted 05 November 2009 - 07:58 AM
GSTAR, on 05 November 2009 - 07:36 AM, said:
The code of the view:
<p>Displaying Properties <?php echo $init.'-'.$end.' of '.$total_count; ?></p>
you should handle a default pageSize
if (isset($_POST['pageSize']) and !empty($_POST['pageSize']))
$pageSize=$_POST['pageSize'];
else
$pageSize= 20;
put ALL code (controller and view)
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
#16
Posted 05 November 2009 - 08:39 AM
$init=(($page-1)* $_GET['$pageSize'] )+1;
$end=$init + $_GET['pageSize'];
OK so now I'm using GET, however this is causing problems because I have SEO friendly URLs, so I sometimes get this kind of URL when I submit the form:
/mysite/search/results/pageSize/40/sort/results?pageSize=10&yt0=Update
---
/mysite/protected/views/search/results.php:
<?php echo CHtml::beginForm($action='results', $method='get'); ?>
<?php echo CHtml::dropDownList('pageSize',$_GET['pageSize'],array(
'10'=>'10 per page',
'20'=>'20 per page',
'30'=>'30 per page',
'40'=>'40 per page',
'50'=>'50 per page',
)); ?>
<?php echo CHtml::submitButton('Update'); ?>
<?php echo CHtml::endForm(); ?>
<?php $this->widget('CLinkPager',array('pages'=>$pages)); ?>The CLinkPager produces SEO friendly URLs but the form is appending query parameters to the URL.
#17
Posted 09 November 2009 - 07:29 AM
#18
Posted 10 November 2009 - 07:10 AM

Help
This topic is locked











