Hi BFox,
I had the same issue as you; building a dynamic menu with data from
the database.
The solution is very simple, yet it's quite heavy on the server
(because it loads every single pageview).
<b>app/controller/menus_controller.php</b>
<pre>
<?php
class MenusController extends AppController
{
var $name = 'Menus';
var $components = array ();
var $helpers = array('Html');
var $layout = 'empty';
function getMenus($location = null) {
if($location != null) {
$data = $this->Menu->findAll(array('location' =>
$location));
return $data;
}
}
function getSubMenu($parent = null) {
if($parent != null) {
$data = $this->Menu->findAll(array('parent' =>
$parent));
$this->set('subs',$data);
}
}
}
?>
</pre>
<b>app/models/menu.php</b>
<pre>
<?php
class Menu extends AppModel
{
var $name = 'Menu';
}
?>
</pre>
<b>app/views/elements/menu.thtml</b>
<pre>
<ul>
<?php
$menus = $this->requestAction('menus/getMenus/header'); // last
parameter (header) defines the location (see SQL)
$cnt = count($menus) - 1;
$cntr = 0;
foreach ($menus as $menu) {
?>
<li>
<?php
echo $html->link($menu['Menu']['title'], $menu['Menu']['url'],
(($cnt == $cntr) ? array('class' => 'last') : '' ) );
$cntr++;
?>
</li>
<?php
}
?>
</ul>
</pre>
<b>SQL</b>
<pre>
CREATE TABLE `menus` (
`id` int(11) unsigned NOT NULL auto_increment,
`title` varchar(255) collate latin1_general_ci NOT NULL default '',
`url` varchar(255) collate latin1_general_ci NOT NULL default '',
`location` enum('header') collate latin1_general_ci NOT NULL default
'header', # add more values when you have multiple menu locations
`order` int(11) NOT NULL default '0',
`active` enum('true','false') collate latin1_general_ci NOT NULL
default 'true',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
AUTO_INCREMENT=2 ;
INSERT INTO `menus` VALUES (1, 'Homepage', '/', 'header', 1, 'true');
</pre>
And last but not least you need to add the following line at your
default html (eg. app/views/layout/default.thtml)
<pre>
<?php echo $this->renderElement('menu'); ?>
</pre>
I hope this will help you out (or atleast give you a start to create
your own).
Johan de Jong
PS I hope you can read the PRE texts...
On 26 dec, 23:06, BFox <[EMAIL PROTECTED]> wrote:
> Im working on a project that was originally developed by another PHP
> developer. He created a helper class to create the main menu in such a
> fashion...
>
> Function A. Would actually add the menu items to an array with the
> Anchor Tag, URL and a Key
>
> Function B. Would iterate through each item in the array and create
> the link with the Anchor Tag and URL, it would also check if the
> passed "Selected" item was equal to the key for each array item, if
> so, it puts an "Active" CSS class on it.
>
> This system works using $this->renderElement in the view file as the
> element file contains the functions that adds each menu item and
> finally does a print() on the menu function with the selected item
> being passed to it.
>
> My current project requires that for some of the menu items they have
> sub items that are pulled from the DB. I have found that I cant call a
> function from a model from the element file because I get an
> "unrecognized function in a view file" or something similar.
>
> Is there a way I can call a model function from the element or
> otherwise dynamically add menu items from the DB before I do the
> renderElement?
>
> The element looks like this...
>
> $mainMenu->addMenuItem("News and Events","news","/news");
> $mainMenu->addMenuItem("About","about","/about");
> $mainMenu->addMenuItem("Locations","locations","/locations");
> $mainMenu->addMenuItem("Contact","contacts","/contact");
> print($mainMenu->showMenu($selected));
>
> addMenuitem is the function within the helper class that adds items
> and showMenu, well thats self explanitory...
>
> Sorry this post was long and verbose, just wanted to make sure that
> anyone who looks is on the same page.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---