Yii Framework Forum: Pagination / refine search results - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • This topic is locked

Pagination / refine search results Rate Topic: -----

#1 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 02 November 2009 - 05:23 AM

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?
0

#2 User is offline   Horacio Segura 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 02 November 2009 - 06:56 AM

View PostGSTAR, on 02 November 2009 - 05:23 AM, said:

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

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
KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
0

#3 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 02 November 2009 - 08:09 AM

Hi there,

Thanks. For number 3, can you advise where $page, $pageSize and $total_count are declared?
0

#4 User is offline   Horacio Segura 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 02 November 2009 - 08:50 AM

View PostGSTAR, on 02 November 2009 - 08:09 AM, said:

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

KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
0

#5 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 02 November 2009 - 08:58 AM

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

#6 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 02 November 2009 - 09:09 AM

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,
	));
}

0

#7 User is offline   Horacio Segura 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 02 November 2009 - 10:07 AM

View PostGSTAR, on 02 November 2009 - 09:09 AM, said:

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;
$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'];




KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
0

#8 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 03 November 2009 - 03:31 AM

OK that works a little better but still not quite right, I get "Displaying results 1-1 of 500" on every page.
0

#9 User is offline   Horacio Segura 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 03 November 2009 - 02:41 PM

View PostGSTAR, on 03 November 2009 - 03:31 AM, said:

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
KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
0

#10 User is offline   Horacio Segura 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 03 November 2009 - 02:42 PM

View PostGSTAR, on 03 November 2009 - 03:31 AM, said:

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
KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
0

#11 User is offline   Horacio Segura 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 03 November 2009 - 02:43 PM

View PostGSTAR, on 03 November 2009 - 03:31 AM, said:

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
KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
0

#12 User is offline   ngocnq 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 21-July 09

Posted 04 November 2009 - 10:26 PM

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
0

#13 User is offline   Horacio Segura 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 05 November 2009 - 05:00 AM

View Postngocnq, on 04 November 2009 - 10:26 PM, said:

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


KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
0

#14 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 05 November 2009 - 07:36 AM

View PostHoracio Segura, on 03 November 2009 - 02:43 PM, 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


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>

0

#15 User is offline   Horacio Segura 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 05 November 2009 - 07:58 AM

View PostGSTAR, on 05 November 2009 - 07:36 AM, said:

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)
KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
0

#16 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 05 November 2009 - 08:39 AM

OK I'm getting closwer now, I made the following changes:

$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.
0

#17 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 09 November 2009 - 07:29 AM

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

#18 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 10 November 2009 - 07:10 AM

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?
0

#19 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 11 November 2009 - 08:55 AM

Can someone please help me with this!
0

#20 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 16 November 2009 - 10:14 AM

BUMP!
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users