Pagination / refine search results

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?

  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*/
  1. 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

  2. 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

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*/



Also I’m using POST, so regarding number 2 how can I apply the sort order for POST submitted values?

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,

	));

}

error typo, try again with code modify

the error




$pages->pageSize=$_POST[pageSize];

$pages->pageSize=$_POST['pageSize'];







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

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




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’]))

&#036;pageSize=&#036;_POST['pageSize'];

else

&#036;pageSize= 20;

put ALL code (controller and view)

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.

Is it possible to make the CLinkPager use GET format for its generated URLs? I’m using PATH format as a global setting.

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?

Can someone please help me with this!

BUMP!