> Q. How do I create navigation / links based on the entries in a
> database?
> In the cats controller:
> function navigation()
> {
> $this->Cats->recursive = 0;
> $this->set('navdata', $this->Cats->findAll(null, array('name',
> 'id')));
> $this->render();
> }
>
> In the navigation.thtml:
> <ul>
> <?php foreach($navdata as $nav): ?>
> <li><?php echo $html->link($nav['Cat']['name'], "/Cats/view/".
> $nav['Cat']['id']); ?></li>
> <?php
> endforeach;
> ?>
> </ul>
>
> In the default.thtml:
> $this->requestAction('/cats/navigation/');
I'm not 100% sure that would be considered the best approach, for 2 reasons:
1. you're using Cat->findAll() with restricted fields when really you
want Cat->generateList();
2. using requestAction is another page request, which incurs more of a
hit to the server as it's effectively the same as visting a page on
the site.
I think a better approach would be http://bin.cakephp.org/view/255041463 :
<?php
// AppController
var $uses = array('Cat');
function beforeFilter()
{
$cats = $this->Cat->generateList();
$this->set('navdata', $cats);
}
// default.thtml
<ul>
<?php foreach($navdata as $nav): ?>
<li><?php echo $html->link($nav['Cat']['name'],
"/Cats/view/".$nav['Cat']['id']); ?></li>
<?php endforeach; ?>
</ul>
?>
this could be extended to use cachAction as well, helping to
demonstrate realworld cake caching. It could also be fleshed out into
a Component/Helper combination, to help demonstrate code re-use.
hth
jon
--
jon bennett
t: +44 (0) 1225 341 039 w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---