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

then…

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->set('str','');   

   }   

  else   

   {   

    $this->Gallery->recursive = 0;   

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

    $this->set('str',$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

<?php   

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

?>
<?php
if(!empty($str))   

 {
?>   

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

<tr>   

    

 <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'])?>   

 </td>   

</tr>   

<?php endforeach; ?>   

</table>   

<?php
 }
?>
 

Step 3 – create the element

<?php
 $alphabet = array('a','b','c','d','e','f','g','h','i','j', 
'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'); 
  
 echo('<ul class="atozlist">'); 
 $i = 1; 
 foreach($alphabet as $link) 
  { 
   $class = ''; 
   if($i == 1) 
    { 
     $class = ' class="firstitem" '; 
     $i++; 
    } 
     
   if(strtolower($str) == $link) 
    { 
      echo('<li ' . $class . '> 
           <span class="thisitem">' . $link . '</span></li>'); 
    } 
   else 
    { 
     echo('<li ' . $class . '><a href="' . $url . $link . '">
        ' . $link . '</a></li>'); 
    } 
  } 
 echo('</ul>'); 
?>

Cook up Web sites fast with CakePHP

I was reading the Google Group for Cake PHP today, one of the posts mentioned a link to a really good set of tutorials about Cake, which I have been reading this afternoon. You need to register for an IBM id but it definately worth it – I wish I had found out about the articles a few weeks ago.

http://www-128.ibm.com/developerworks/edu/os-dw-os-php-cake1.html

So far I’ve only read the first 3 articles but the it is very well writen, particularly the information about ACLs.