GSTAR
(Omzy83)
November 2, 2009, 10:23am
1
I need the following functionality on my search results page:
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?
I need the following functionality on my search results page:
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?
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*/
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
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
GSTAR
(Omzy83)
November 2, 2009, 1:09pm
3
Hi there,
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*/
GSTAR
(Omzy83)
November 2, 2009, 1:58pm
5
Also I’m using POST, so regarding number 2 how can I apply the sort order for POST submitted values?
GSTAR
(Omzy83)
November 2, 2009, 2:09pm
6
It doesn’t work for me. Here is my code:
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,
));
}
GSTAR:
It doesn’t work for me. Here is my code:
public function actionResults()
{
if (!isset($_GET['page']))
{
$page=1;
}
else
{
$page=$_GET['page'];
}
$criteria=new CDbCriteria;
[b] $total_count=Property::model()->count($criteria);[/b]
$pages=new CPagination([b]$total_count[/b]);
//$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'];
GSTAR
(Omzy83)
November 3, 2009, 8:31am
8
OK that works a little better but still not quite right, I get "Displaying results 1-1 of 500" on every page.
$_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
$_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
$_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
ngocnq
(Nquangngoc)
November 5, 2009, 3:26am
12
i’m too, i show page-1, but i don’t show page-2
because $_POST[’…’] don’t send when I click to page-2.
sorry my english is very poor
ngocnq:
i’m too, i show page-1, but i don’t show page-2
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
GSTAR
(Omzy83)
November 5, 2009, 12:36pm
14
$_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
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>
GSTAR:
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>
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)
GSTAR
(Omzy83)
November 5, 2009, 1:39pm
16
OK I’m getting closwer now, I made the following changes:
[b]$init=(($page-1)* $_GET[’$pageSize’] )+1;
$end=$init + $_GET[‘pageSize’];[/b]
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.
GSTAR
(Omzy83)
November 9, 2009, 12:29pm
17
Is it possible to make the CLinkPager use GET format for its generated URLs? I’m using PATH format as a global setting.
GSTAR
(Omzy83)
November 10, 2009, 12:10pm
18
Does anyone know how to make the CLinkPager use the query string format for it’s generated URLs (rather than path format) WITHOUT switching off PATH format in the global setting?
GSTAR
(Omzy83)
November 11, 2009, 1:55pm
19
Can someone please help me with this!