A to Z links with CakePHP

On a lot of websites, especially directories or those which contain large amounts of alphabetical data you see A to Z lists. You know the idea, click on a A and see every entry beginning with A, click on B… etc.A to Z screenshot


A to Z list

Well, here is how to do it with Cake (a new PHP application development framework). It isn’t at all hard.

First some prerequisites:

  1. A working cake setup.
  2. A simple model storing some data in the corresponding database table – in our case called Gallery.

CREATE TABLE `gallery` (

`id` int( 10 ) unsigned NOT NULL AUTO_INCREMENT ,
`title` varchar( 50 ) default NULL ,
`body` text,
`created` datetime default NULL ,
`modified` datetime default NULL ,
PRIMARY KEY ( `id` )


Step 1 – edit the gallery controller

You need to a new function to your gallery controller in order to show the A to Z link list. It should look something like this:

function show($str = null ) {  if(!$str)   






    $this->Gallery->recursive = 0;   

    $this->set('galleries', $this->Gallery-> 
                     findAll("LEFT(title, 1) = '$str'"));   





Step 2 – create the view

You now need to create a view for this action, your view will should be called show.thtml and should be created in app\views\galleries. This is pretty simple – the only thing to take note of is right at the top – renderElement – rather than put the code to write the A to Z list directly in the view, I’ve decided to create it in an element. This means that it can be reused in any view within the site.

The first parameter ‘atoz‘ is the name of the element which in this case will be called atoz.thtml the second parameter is an array containing the varibales that will be available to the element – url is the action we want the links to point to, str is the value of str we selected e.g. b


   echo $this->renderElement('atoz', array('url' => 
'/galleries/show/', 'str' => $str));   



<table cellpadding="0" cellspacing="0">
<?php foreach ($galleries as $gallery): ?>   



 <td><?php echo $gallery['Gallery']['title']; ?></td>   

 <td><?php echo $gallery['Gallery']['body']; ?></td>   


 <td nowrap>   

  <?php echo $html->link('View', 
      '/galleries/view/' . $gallery['Gallery']['id'])?>   



<?php endforeach; ?>   



Step 3 – create the element

 $alphabet = array('a','b','c','d','e','f','g','h','i','j', 
 echo('<ul class="atozlist">'); 
 $i = 1; 
 foreach($alphabet as $link) 
   $class = ''; 
   if($i == 1) 
     $class = ' class="firstitem" '; 
   if(strtolower($str) == $link) 
      echo('<li ' . $class . '> 
           <span class="thisitem">' . $link . '</span></li>'); 
     echo('<li ' . $class . '><a href="' . $url . $link . '">
        ' . $link . '</a></li>'); 

3 thoughts to “A to Z links with CakePHP”

  1. This is starting to look interesting. Once I have jumped over the boundries of website development and programming, and I actually begin to see things as they should be, I’m interested in looking at this.

    Something I meant to ask you is, is there a quick way is displaying code -as- code on a webpage.

    Well, there obviously is. If you want to show someone how to write some XHTML, and that XHTML has some paragraph tags in it, how do you make the browser and server ignore that as code and display it as text, if you know how I mean?

  2. You can use <code> and <pre> but neither is perfect – there are also a couple of PHP functions.

    Check this out: http://www.sitepoint.com/article/highlight-source-code-php

    Anyway I want to get a WordPress plugin to do it all for me. I just upgraded to the latest version and it’s good – a real improvement, although there are still a few issues to sort out in my blog stylesheets etc.

Comments are closed.