logo

Alphabetical Pagination / Sort by in CakePHP

logo

CakePHP has some great pagination functions built in – however these are normally categorised with “sortby” and “Number per page”.

I found on another blog this code (http://foldifoldi.com/news/?p=169):

function index($filter = null) {
	$this->SttwTrack->recursive = 0;
 
	$this->set('filter', $filter);
 
     // query all distinct first letters used in names
     $letters = $this->SttwTrack->query('SELECT DISTINCT SUBSTRING(`title`, 1, 1) FROM `sttw_tracks` ORDER BY `title`');
 
     $links = array();
     // push all letters into a non-nested array
     foreach ($letters as $row) {
          array_push($links, current($row[0]));
     }
 
     $this->set('links', $links);
 
     $this->paginate['SttwTrack'] = array(
     'limit' => 10,
     'order' => array(
          'SttwTrack.title' => 'asc'
      )
	);
 
	$data = $this->paginate('SttwTrack',
	     array(
	          'SttwTrack.title LIKE' => $filter .'%'
	     )
	);
 
	$this->set('data', $data);
	$this->set('filter', $filter);
	$this->set('sttwTracks', $this->paginate());
}

The code above basically creates a custom query to find the first letter of each title from the SttwTrack model, it then adds each of these into an array to be passed to the view.

in your view the following code should display the Alphabet list and link any of the letters that have titles in them.

// set url arguements
$paginator->options(array('url' =>  array($filter)));
 
// render the previous link
echo $paginator->prev('prev',null,null,null);
 
// set up your alphabet
$alpha = range('A','Z');
echo "<ul class=\"sortby\">";
for ($i=0; $i < count($alpha); $i++) {
	echo "<li>";
	// if current letter is not in the links array, do not make it clickable
	if(!in_array($alpha[$i], $links)) {
		echo"<span class='nolink'>" . $alpha[$i] . "</span>";
	} else {
		if ($alpha[$i] == $filter) {
			// if the current letter matches the filter, give it a selected style
			echo $html->link($alpha[$i], array('controller' => strtolower($this->params['controller']), 'action' => 'index', $alpha[$i]), array('class' => 'link_selected')) . "";
		} else {
			echo $html->link($alpha[$i], array('controller' => strtolower($this->params['controller']), 'action' => 'index', $alpha[$i]), array('class'=>'link')) . "";
		}
	}
	echo "</li>";
}
echo "</ul>";
// render the 'next' link
echo $paginator->next('next', null, null, null);

I know its just a snippet but hopefully it should make some sense to you :)

Share with your friends and help out this site:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogosphere News
  • E-mail this story to a friend!
  • Identi.ca
  • LinkedIn
  • Live
  • MisterWong
  • MySpace
  • Ping.fm
  • Reddit
  • RSS
  • Slashdot
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Tumblr
  • Twitter
  • Yahoo! Bookmarks
  • Yahoo! Buzz
logo
logo
Powered by Nu Order Webs