cakephp pagination with search result


For make pagination compatible with search results

In controller file


<?php
if(isset($this-&gtrequest->data)) {
if (isset($this->request->data['User']['Search']) && ($this->request->data['User']['Search'] != '')) {
$search_keyword = $this->request->data['User']['Search'];
}
}
/*if search keyword is not enter then look for pagination condition params*/
elseif (!empty($this->request->params['named']['cond'])) {

$this->request->params['named']['cond'] = strtr($this->request->params['named']['cond'], '-_, ', '+/=');

$this->request->params['named']['cond'] = json_decode(base64_decode($this->request->params['named']['cond']), true);
if (isset($this->request->params['named']['cond']['search_keyword']) && $this->request->params['named']['cond']['gender'] != '') {
$search_keyword = trim($this->request->params['named']['cond']['search_keyword']);
}
}

if(!empty($search_keyword)){
$cond[] = "Product.productname LIKE '%" . $search_keyword . "'";
$separator['search_keyword'] = $search_keyword;
}

if (!empty($this->request->params)) {
if (isset($this->request->params["named"]["page"])) {
$urlSeparator[] = 'page:' . $this->request->params["named"]["page"];
}
if (isset($this->request->params["named"]["sort"])) {
$urlSeparator[] = 'sort:' . $this->request->params["named"]["sort"];
}
if (isset($this->request->params["named"]["direction"])) {
$urlSeparator[] = 'direction:' . $this->request->params["named"]["direction"];
}
}

$urlSeparator = implode("/", $urlSeparator);
$this->set('separator', $separator);
$this->set('urlSeparator', $urlSeparator);

$this->set('Products', $this->paginate("Product", $cond));

?>

and in ctp file


<?php

$this->Paginator->options(array('update' => '#div_id_for_update',
'url' => array('controller' => 'controller_name', 'action' => 'index', "cond" => strtr(base64_encode(json_encode($separator)), '+/=', '-_,')),
));

?>

Here in controller first we check if user click on search button then check for $this->request->data and set varriable $search_keywrd else check for pagiantion params and set $search_keyword and set pagination varriable and then in ctp file using $this->pagination->options set pagination link url with search condition.

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