How to implement pagination with search in CakePHP - cakephp

My pagination is working, even the search is also working. But the problem that I'm having is when i click on next page link in the pagination links. The search is not working for the next page of the pagination. Also I need to know how I send other parameters through the url, and use them in query of pagination. I need help on this as I am novice in CakePHP.
In controller page I have used this code:
class StatesController extends AppController {
public $components = array('Paginator');
public $paginate = array(
'limit' => 2,
'fields' => array('State.id', 'State.state','State.code'),
'order' => array(
'State.state' => 'asc'
)
);
public function admin_index() {
$this->layout = false;
$this->layout = 'adminlayout';
//****** pagination starts
$search=$this->request->data('State.search');
$this->Paginator->settings = $this->paginate;
// similar to findAll(), but fetches paged results
$stateListAr = $this->Paginator->paginate('State',
array('State.state LIKE' => "%".$search."%")
);
$this->set('stateListAr', $stateListAr);
//****** pagination ends
$this->set('stateListAr',$stateListAr);
$this->render('admin_index');
}
}
In view page I have used this code:
<?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $this->Paginator->numbers(array('first' => 'First page')); ?>
<?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?>

i have got solution to my problem , here it is . I have used a code in view page , here $search variable i have setted data from controller.
$search);
$this->Paginator->options(array(
'url' => $urlParamAr
));
echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled'));
echo $this->Paginator->numbers(array('first' => 'First page'));
echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
?>

Related

cakephp paginator last record delete error

Sample here, please check that happens or not
<?php
echo $this->Paginator->prev
($this->Html->image('prev.png'), array('escape' => false),
array(), null, array('class' => 'prev'));
echo $this->Paginator->counter
('Page {:page} of {:pages}, Total Records {:count}');
echo $this->Paginator->next($this->Html->image
('next.png'), array('escape' => false),
array(), null, array('class' => 'next'));
?>
Check out this, hope it works
public function index() {
try {
$paginatedData = $this->Paginator->paginate();
} catch (NotFoundException $e) {
//get current page
$page = $this->request->params['named']['page'];
if( $page > 1 ){
//redirect to previous page
$this->redirect( array( "page" => $page-1 ) );
}else{
$paginatedData = array(); //no data to paginate so use empty array()
//you will have to check for this in the view and no longer display the pagination links, since they will NOT be defined
}
}
}

How to customize CakePHP pagination url?

currently i am using cakephp version 2.5.3
I want to change my cakephp pagination url.
My current URL is http://www.example.com/newsFeeds/ajax_news_feed/page:2 I need http://www.example.com/newsFeeds/index/page:2
My Code:
<?php
echo $this->Paginator->prev(' <<' . __('Previous '), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers();
//echo $this->Paginator->url(array('controller'=>'newsFeeds', 'action'=>'index'));
//echo $this->Paginator->link('Sort by title on page 5', array('controller'=>'newsFeeds', 'action'=>'index'));
echo $this->Paginator->next(__(' Next') . '>> ', array(), null, array('class' => 'next disabled'));
?>
Above pagination is showing-
When i am clicking 2 then the link is going to http://www.example.com/newsFeeds/ajax_news_feed/my_post/page:2 but i need http://www.example.com/newsFeeds/index/my_post/page:2
Please tell me how to change controller and action in pagination?
User $this->Paginator->options-
Code:
<?php
$this->Paginator->options['url'] = array('controller' => 'newsFeeds', 'action' => 'index/my_post');
echo $this->Paginator->prev(' <<' . __('Previous '), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers();
echo $this->Paginator->next(__(' Next') . '>> ', array(), null, array('class' => 'next disabled'));
?>
For cakephp 3 its a little bit different:
$this->Paginator->options([
'url' => [
'controller' => 'newsFeeds',
'action' => 'index',
'my_post']
]);

How to do multiple pagination without page not found error in Cakephp

I have paginate 2 models (Income,Expanse) in the Student model view page .
I have really page not found problem with cakephp's pagination.
It have no any problem when the paginate result has only 1 page or the same, but It will error if one has more paginate result than other.
for example.
-income has paginate result 1 page and expanse has 1 page. No problem at all.
-income has 10 pages and expanse has 10 pages no problem at all.
-income has 2 pages and expanse has 1 page. income page 2 page not found error.
-income has 5 pages and expanse has 2 pages. income page 3,4,5 page not found error.
-income has 10 pages and expanse has 13 pages. expanse page 11,12,13 page not found error.
for example(not real one) , Student's view have income and expense items ,Both are display as pagination.
//this is how I config paginator in Student controller.
public $paginate = array(
'Income' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'recursive'=>0
),
'Expense' => array (
'order'=>array('expense_id'=>'ASC'),
'limit'=>10,
'recursive'=>0,
)
);
<div class="paging">//this is how I config Income paginator in Student view
<?php
echo $this->Paginator->prev('< ' . __('previous'), array('model'=>'Income'), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('model'=>'Income','separator' => '','modulus'=>100,'first'=>'หน้าแรก','last'=>'หน้าสุดท้าย'));
echo $this->Paginator->next(__('next') . ' >', array('model'=>'Income'), null, array('class' => 'next disabled'));
?>
</div>
//this is how I config Expanse paginator in Student view
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array('model'=>'Expanse'), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('model'=>'Expanse','separator' => '','modulus'=>100,'first'=>'หน้าแรก','last'=>'หน้าสุดท้าย'));
echo $this->Paginator->next(__('next') . ' >', array('model'=>'Expanse'), null, array('class' => 'next disabled'));
?>
</div>
Please help me. sorry for my english
If you have any question , please ask me.
Thank you.
Yes It is possible.....
Step1 : Make paging.ctp in View/Elements/paging.ctp
<?php if (!isset($this->Paginator->params['paging'])) {
return;
}
if (!isset($model) || $this->Paginator->params['paging'][$model]['pageCount'] < 2) {
return;
}
if (!isset($options)) {
$options = array();
}
$options['model'] = $model;
$options['url']['model'] = $model;
$this->Paginator->__defaultModel = $model;
?>
<div class="paging">
<ul class="pagination pull-right">
<?php
echo $this->Paginator->prev('<<', array_merge(array('class' => '', 'tag' => 'li'), $options), null, array('class' => 'disabled', 'tag' => 'li'));
echo $this->Paginator->numbers(am($options,array('tag' => 'li', 'separator' => '', 'currentClass' => 'active', 'currentTag' => 'a')));
echo $this->Paginator->next('>>', array_merge(array('class' => '', 'tag' => 'li'), $options), null, array('class' => 'disabled', 'tag' => 'li'));
?>
</ul>
</div>
Step2: Now add this function to Controllers/AppController
public function pageForPagination($model) {
$page = 1;
$sameModel = isset($this->request->params['named']['model']) && $this->request->params['named']['model'] == $model;
$pageInUrl = isset($this->request->params['named']['page']);
if ($sameModel && $pageInUrl) {
$page = $this->request->params['named']['page'];
}
$this->passedArgs['page'] = $page;
return $page;
}
Step3: Now in controller action do some conditions so that proper page should be call
if (empty($this->request->params['named'])) {
$page = $this->pageForPagination('Income');
$page1 = $this->pageForPagination('Expense');
public $paginate = array(
'Income' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'page' => $page,
'recursive'=>0
),
'Expense' => array (
'order'=>array('expense_id'=>'ASC'),
'limit'=>10,
'page' => $page1,
'recursive'=>0,
));
} else if ($this->request->params['named']['model'] == 'Income') {
$page = $this->pageForPagination('Income');
public $paginate = array(
'Income' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'page' => $page,
'recursive'=>0
));
} else if ($this->request->params['named']['model'] == 'Expense') {
$page1 = $this->pageForPagination('Expense');
public $paginate = array(
'Expense' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'page' => $page1,
'recursive'=>0
));
Step 4: Now call the paging element in your student view
<?php echo $this->element('paging', array('model' => 'Income'));?>
<?php echo $this->element('paging', array('model' => 'Expense'));?>
Note: Please take care of brackets and semicolon.......and sorry to be late but will help others...Thanks..
I think if you use two different iframes to load different URLs for pagination, you can handle this.
OR you have to call ajax functions to paginate 2 models in one page without reloading the page....

Ajax-pagination triggers only once

i implemented the pagination-components according to the official documentation:
http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html
I also included ajax-support. Basically everthing works fine except one thing: If i switch from page 1 to page 2 the ajax-request will work, after that i switch from page 2 to 3 instead a ajax-request a normal Post-Request will be performed. So the ajax-thing works only once.
My controller looks like this (short form):
public $components = array('Paginator', 'RequestHandler');
public $helpers = array('Js', 'Categorie');
....
$this->Paginator->settings = array(
'limit' => '15'
);
No the more important thing is my view: (jquery are included in my layout...)
$this->Js->JqueryEngine->jQueryObject = 'jQuery';
$this->Paginator->options(array(
'update' => '#paginate',
'evalScripts' => true,
'before' => $this->Js->get('#busy-indicator')->effect('fadeIn', array('buffer' => false)),
'complete' => $this->Js->get('#busy-indicator')->effect('fadeOut', array('buffer' => false))
));
?>
<?php
echo $this->Html->image('indicator.gif', array(
'id' => 'busy-indicator'
));
?>
<div id="paginate">...display data...</div>
<?php
echo $this->Paginator->numbers();
echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled'));
echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
echo $this->Paginator->counter();
?>
That's all... as mentioned before: Switch Page 1->2 will work, Switch Page 2-3 will do a normal POST-Request, Switch 3-4 will work and so one...
A bit strange, so any idea? Btw. i tried different jquery-versions without any effect (currently i am using 2.0.3...)
Thanks - Best regards
Teyhouse
In this case, you just needed to also have:
<?php echo $this->Js->writeBuffer(); ?>
In your app/View/Layouts/ajax.ctp file.

CakeDC search plugin pagination not working

Hi I'm currently using the CakeDC search plugin for my CakePHP (2.2 app).
The search facility itself works fine, however I cannot get the results or the data shown on page before the search to paginate.
Heres my code
Model:
// Configure Meio file uploader
var $actsAs = array(
'MeioUpload.MeioUpload' => array(
'filename' => array(
'allowedMime' => array('image/jpeg', 'image/pjpeg', 'image/png', 'application/pdf'),
'allowedExt' => array('.jpg', '.jpeg', '.png', '.pdf'),
'maxSize' => '8 Mb'
)
),
'Search.Searchable'
);
// Search plugin filters
public $filterArgs = array(
'title' => array('type' => 'like'),
'live' => array('type' => 'value'),
'search' => array('type' => 'like', 'field' => 'FaqArticle.description'),
'error' => array('type' => 'like'),
'description' => array('type' => 'like')
);
Controller:
// Search plugin
public $components = array('Search.Prg');
public $presetVars = true; // using the model configuration
public function admin_index() {
$this->Prg->commonProcess();
$this->paginate = array('conditions' => $this->FaqArticle->parseCriteria($this->passedArgs));
$this->set('faqArticles', $this->paginate());
// Count all live articles for intro text
$this->set('liveArticles', $this->FaqArticle->find('count', array('conditions' => array('FaqArticle.live' => '1')
)));
// Count all articles for intro text
$this->set('countedArticles', $this->FaqArticle->find('count'));
// Set searched for details
$this->set('searchedFor', $this->passedArgs);
// Set layout
$this->layout = 'admin';
}
View:
<div class="pagination">
<ul>
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('tag' => 'li', 'separator' => '', 'currentClass' => 'active', 'modulus' => '5'));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</ul>
this code:
public $components = array('Search.Prg');
public $presetVars = true; // using the model configuration
Should be in your controller, not your model. Models don't have 'components'. Controllers have 'components'. And check the doco for the Serach plugin - the $filterArgs goes in the model (which you've done correctly), but the $presetVars goes in the controller.
Don't set $presetVars as an empty array. Just move it from your model to your controller. It should be declared up the top as a public var, as you've done in your model, and shouldn't be declared inside the admin_index method.

Resources