Search results with pagination CakePHP

//add these lines in your view file
<?php
$newUrl = “list”.$urlString;
$urlArray = array(
‘srch_name’ => $search1,
‘srch_type’ => $search2
);

$this->Paginator->options(array(‘url’=>$urlArray));

?>
//create form in the same view file
<?php echo $this->Form->create(‘User’,array(‘action’=>$newUrl,’method’=>’POST’,”class”=>”form”,’name’=>’listForm’));?>
<?php echo $this->Form->input(“name”,array(“class”=>”text2 inputClickBlur”,”label”=>false,”div”=>false,”value”=>$search1,”alt”=>”Type name here”)); ?>
<?php $options=array(‘MNG’=>’Manager’,’SUP’=>’Supplier’,’email’=>’Email’);
echo $this->Form->select(“type”,$options,array(“class”=>”short dropdownClick text3″,”label”=>false,”div”=>false,’empty’=>’—Select—-‘),$search2);?>
<?php echo $this->Form->submit(‘Search’,array(‘title’=>”Search”,’class’=>”normal_btn dblclick”,’div’=>false,’style’=>’margin:1px 5px 0 5px’)); ?>
<?php echo $this->Form->button(“Reset”,array(‘title’=>”Reset”,”type”=>”button”,”class”=>”normal_btn”,’div’=>false,”label”=>false,”onclick”=>”location.href='”.BASE_URL.”admin/users/list'”));?>

//apply sorting if you want to use on fields in same view file

<?php echo $this->Paginator->sort(‘User.firstname’,’Name’);?>
<?php echo $this->Paginator->sort(‘User.email’,’Email’);?>
<?php echo $this->Paginator->sort(‘User.user_type’,’Role’);?>
<?php echo $this->Paginator->sort(‘User.status’,’Status’);?>

//add following code in your controller method of above view
<?php

$criteria = “1”; //All Searching
$this->set(“search1″,”Type name here”);
$this->set(“search2”, “”);
$this->Session->delete(‘SESSION_SEARCH’);

$search2=””;
$search1=””;
$criteria=array(‘User.user_type !=’=>’SADM’);
if(isset($this->data[‘User’]) || !empty($this->params[‘named’]))
{
if((!empty($this->data[‘User’][‘name’]) && $this->data[‘User’][‘name’] != “Type name here”)|| (!empty($this->params[‘named’][‘srch_name’]) && $this->params[‘named’][‘srch_name’]!= ‘Type name here’))
{
if(isset($this->data[‘User’][‘name’] )){
$search1 = trim($this->data[‘User’][‘name’]);
}elseif(isset($this->params[‘named’][‘srch_name’])){
$search1 = trim($this->params[‘named’][‘srch_name’]);
}
$this->set(“search1″,$search1);
$pos = strpos($search1,’@’);
if(!empty($pos))
{
// $criteria .= ” AND User.email='”.$search1.”‘”;
$criteria = array_merge($criteria, array(‘User.email LIKE’=>’%’.$search1.’%’));
}
else
{
$criteria = array_merge($criteria, array(‘User.firstname LIKE’=>’%’.$search1.’%’));
}
}

if(!empty($this->data[‘User’][‘type’]) || !empty($this->params[‘named’][‘srch_type’])){
if(isset($this->data[‘User’][‘type’])){
$search2 = trim($this->data[‘User’][‘type’]);
}elseif(isset($this->params[‘named’][‘srch_type’])){
$search2 = trim($this->params[‘named’][‘srch_type’]);
}
$this->set(“search2″,$search2);
$criteria = array_merge($criteria, array(‘User.user_type LIKE’=>’%’.$search2.’%’));
}
//Done for fixing searching during pagination
$action=””;
if(isset($this->params[‘named’]) && !empty($this->params[‘named’][‘page’])){
$no_of_records=$this->User->find(‘count’,array(‘conditions’=>$criteria));
$no_of_pages=ceil($no_of_records/3);
if($no_of_pages<$this->params[‘named’][‘page’]){
$action=’list/srch_name:’.$search1.’/srch_type:’.$search2;
$this->redirect(array(‘controller’=>’users’,’action’ =>$action));
}
}
}
// echo ‘<pre>’; print_r($this->params);
if(isset($this->params[‘named’])){
$urlString = “/”;
$completeUrl = array();

if(!empty($this->params[‘named’][‘page’]))
$completeUrl[‘page’] = $this->params[‘named’][‘page’];

if(!empty($this->params[‘named’][‘sort’]))
$completeUrl[‘sort’] = $this->params[‘named’][‘sort’];

if(!empty($this->params[‘named’][‘direction’]))
$completeUrl[‘direction’] = $this->params[‘named’][‘direction’];
if(isset($search1))
$completeUrl[‘srch_name’] = $search1;
if(isset($search2))
$completeUrl[‘srch_type’] = $search2;
foreach($completeUrl as $key=>$value){
$urlString.= $key.”:”.$value.”/”;
}
}
// pr($criteria);
// echo $urlString;
$this->set(‘urlString’,$urlString);
if(!empty($setValue)){
if(isset($this->params[‘form’][‘IDs’])){
$saveString = implode(“‘,'”,$this->params[‘form’][‘IDs’]);
}

if($saveString != “”){
$this->User->updateAll($setValue,”User.user_id in (‘”.$saveString.”‘)”);
$this->Session->setFlash($messageStr, ‘layout_success’);
}

}

$this->paginate = array(
‘fields’ => array(
‘User.user_id’,
‘User.firstname’,
‘User.user_type’,
‘User.lastname’,
‘User.status’,
‘User.email’,
),
‘page’=> 1,’limit’ => 3,
‘order’ => array(‘user_id’ => ‘desc’)
);

$data = $this->paginate(‘User’,$criteria);

$this->set(‘total_users’, $data);

//my controller method is admin_list,User model and admin_list.ctp view file

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s