Alphabetic Pagination in CakePHP

Open your controller file,

function index($page = 1) {
 $alphabets = range('A', 'Z');
 $alphagroups = array_chunk($alphabets,3);
 $alphagroups[] = array(0,9);
 $cur_alpha = (isset($page))?($page-1):0;
 if($cur_alpha >= 9){
  $reg_cond = '0123456789';
 } else {
  $reg_cond = trim(implode('', $alphagroups[$cur_alpha]));
 }
 $this->set('cur_alpha', $cur_alpha);
 $this->set('alphas', $alphagroups);
 $total = $this->Vendor->find('count');
 $this->set('total', $total);
 $this->Vendor->recursive = -1;
 $vendors = $this->Vendor->find('all', array(
  'conditions' => array('Vendor.name REGEXP' => '^['.$reg_cond.']')
));
 $this->set('vendors', $vendors);
}

Open index.ctp

foreach ($alphas as $key=>$alpha):
 $alphacode = implode("-",$alpha);
 if($key == $cur_alpha):
    echo $alphacode;
 else: 
    echo $html->link(__($alphacode, true), array('action'=>'index', $key+1));
 endif;
endforeach;
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