Pagination with search in Cakephp 2
Hello friends, today I am going to tell you how to use searching with pagination in Cakephp 2. If you want to search multiple things like first name, last name, email etc then it can be done easily. Lets begin searching using pagination.
Step 1: Create Form in view search.ctp:
<?php echo $this->Form->create('User',array('class'=>'searchForm','role'=>'search','autocomplete'=>'off'));
echo $this->Form->input('keyword',array('class'=>'form-control input-sm','maxlength'=>"64",'placeholder'=>' Search your consultants...','div'=>false,'label'=>false)); echo $this->Form->submit('Search');
echo $this->Form->end();
?>
Step 2: Create function in controller and enter the following lines of code:
function search()
{
if(!empty($this->request->data['User']['keyword'])){
$cond=array();
$cond['User.first_name LIKE'] = "%" . trim($this->request->data['User']['keyword']) . "%";
$cond['User.last_name LIKE'] = "%" . trim($this->request->data['User']['keyword']) . "%";
$cond['User.email LIKE'] = "%" . trim($this->request->data['User']['keyword']) . "%";
$conditions['OR'] = $cond;
$this->request->params['named']['User.keyword'] = $this->request->data['User']['keyword'];
}
$this->paginate=array('conditions'=>$conditions,'limit'=>'10');
$result = $this->paginate('User');
$this->set('data',$result);
}
Now add pagination lines in search.ctp
<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>
Thanks for reading
0 Comment(s)