How do I get PassedArgs to work in Pagination in CakePHP? - cakephp

I have a page which returns results that are filtered on an organization_id. The URL looks like this:
nameofview/organization_id:1/page:2/
I'm using the built in Paginator controls and I pass $this->PassedArgs into it like this:
<div class="paging">
<?php echo $paginator->prev('<< '.__('previous', true), array('url' => $this->PassedArgs), null, array('class'=>'disabled'));?>
| <?php echo $paginator->numbers(array('url' => $this->passedArgs));?>
<?php echo $paginator->next(__('next', true).' >>', array('url' => $this->passedArgs), null, array('class'=>'disabled'));?>
</div>
The links look good for the "numbers" but don't work for Next and Previous. The links for both are taking me back to the same page. I think it is because it is passing the "Page" param.
Anyone have an idea how I can pass the correct args to $paginator->numbers?
I tried $this->passedArgs['organization_id'] but that returns errors.

Try this at the top of your view:
$paginator->options(array('url' => $this->passedArgs));
That way, you can drop array('url' => $this->passedArgs) from your prev/next/numbers lines, it should work just fine.

Related

cakephp two paginations on one controller and one view

I have a controller that has two paginations which is reflected in my view but anytime I want to click on page 2, the controller doesnt know which page it is clicking on and I get an error.
class AsController extends AppController
{
public function view ($id = null)
{
$data1 = $this->paginate('Post','Post.foriegn_id' => $id);
$data1 = $this->paginate('Author','Author,foreign_id' => $id);
// rest of the code goes here
}
}
I have two sections in my view page
<div class="post view"> and div class="author view"
And in each of tehse sections I have the following code:
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>
Please assist me in any way to rectify this issue
Cheers
It looks like its more of a structure/design issue. You are using the same id to get posts and authors. Do you have a relationship between posts and authors you can just paginate through one of the models? From what I know the pagination built into cake is meant to work with one model per request. Especially due to how the pagination helper uses the url params. Do you have a case where you want to show page 1 of posts and page 2 of authors? Also in your view action you have your second paginate() saving to the same $data1 var which is overwriting the previous. Not sure if this is a typo.
I would suggest only using one main section of pagination per page. If the data of the authors relates to the posts I would suggest containing and displaying it in the same paginated section.

Add class to pagination links at CakePHP 2.2

I can add a class ("myclass") to the link (previous or next) which does not contain a link.(current page) I just add the class following the default ones:
echo $this->Paginator->prev('< ', array(), null, array('class' => 'prev disabled myClass'));
And it generates something like this:
<span class="prev myClass disabled ">prev</span>
But, when the previous or the next links have a link, then i the class is not added and it generates something like this:
<span class="prev">
prev
</span>
How can i add a class in this cases to this pagination links?
Thanks.
API Says the second parameter is an array for the link options so I guess you could do that:
echo $this->Paginator->prev('< ', array('class' => 'myLinkClass'), null, array('class' => 'prev disabled myClass'));

Passing param through form cakePHP 2.2

I am in a view (view/1) and i want to pass a param to one of my functions on its controller.
I have tried this:
echo $this->Form->create('Post', array('action' => 'move'));
But then, it doesn't pass the param $id which is on the URL.
I have seen it works well on the edit view just doing this:
echo $this->Form->create('Post');
Why is it not working with my view / controller function?
Also, if i try to pass them with something like this:
echo $this->Form->create('Post', array('action' => 'move', 1));
It prints something like this:
<form action="/posts/move" 1="1"
Thanks
To pass the argument, you can simply pass the param in action as:-
echo $this->Form->create('Post', array('action' => 'move/1'));
In Controller,
debug($this->request->params['pass'][0]);
Since edit view already contains the id of the post being edited, we don't need to do explicitly pass the id in form create.
Pass it in Cake style:)
echo $this->Form->create('Post', array('action' => array( 'action' => 'move', 1 ) ));
-or-
echo $this->Form->create('Post', array('url' => Router::url( array( 'action' => 'move', 1 ) ) ));
That will accomplish it while taking routing into account.

cakePHP paginator not passing passedargs

I am using cakePHP and I am trying to get the paginator component to pass the get variables, or passedargs, when you click through to different pages. I have a variety of different search input selectors which "filters" the results returned. This works on first view, but the moment I click on a different page, it shows all of the results.
I have the following setup for my paginator:
// In my controller class:
public $paginate = array('maxLimit' => 10, 'paramType' => 'querystring');
// Within my action method:
$this->paginate = array('conditions' => array(...),
order => array('Model.field ASC'),
'limit' => 20
);
// Calling the paginator:
$results = $this->paginate('Model');
$this->set(compact('results'));
In my view file:
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>
EDIT:
From my understanding it's better to use the passedArgs, but I am a little unsure as to how to do this. My $this->passedArgs returns no results, so I am creating the passed parameters within my controller example. I also changed my form from Get to Post:
$this->passedArgs["searchfield"] = $_POST["value"];
It passes the passedArgs now correctly in the pagination strip, but I am unsure as to how to build the paging conditions array now. In most cases users will not select default values example, one of the filters is date from and date to, and then a search input box, if I leave the dates it will still created the argumens and not return any results so in essence my url would be something like:
http://localhost/site/controller/action/page:3/datefrom:0/dateto:0/searchFor:survey
Any assistance?
You can pass by all parameters in the view with:
$this->Paginator->options(array('url' => $this->passedArgs));
or assign the params manually:
$this->Paginator->options(array('url' => array("0", "1")));
befor echoing the paginator
See the CakePHP Cookbook for further Examples

CakePHP: variables are cleared when next/privous is clicked

I have an action from a controller that provides a result variable via $this->set('found_products', $data);. The view page products.ctp is divided into two sections:
On top, a form where a user enters a string to search $found_products is set for the view.
Below it, paginated results that is displayed only if $found_products is set. i.e. if (isset($found_products)) is true.
When if (isset($found_products)) is true, I get the first page displayed below the form with the search string already in the text box. The URL for this is myapp/controller/products.
The problem occurs when I move into the next pages. The URL becomes myapp/controller/action/products:2 and none of the variables used under myapp/controller/products exist. It looks like moving onto a new page clears all variables.. Below is the code I'm using for paging, and I have no reroute rules written for this. How do I solve this issue?
<div class="paging">
<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
<?php echo $this->Paginator->numbers();?>
<?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
</div>
I've tried to work around this using $_SESSION in the action (products) and setting this for the view, but when I did this, `$this->Paginator' no longer worked.
You can use the $this->Paginator->options to preserve the passed arguments in the pagination links. Try this code
<div class="paging">
<?php $this->Paginator->options(array('url' => $this->passedArgs)); ?>
<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
<?php echo $this->Paginator->numbers();?>
<?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
</div>

Resources