Re: How to build a category-subcategory menu?
this code is for cake TO 1.2. This code will fail on newer versions of cake
--
can you translate this to english?what is maak?--
On Saturday, June 9, 2007 5:01:29 PM UTC+8, Watcher wrote:This is how I fixed this kind of thing:Model:
class Menu extends AppModel {
var $name = 'Menu';// model validation
var $validate = array(
'naam' => VALID_NOT_EMPTY, // backwards compatible
);function maakMenu($belongs_id = '0') {
$menu_voor_view = array();
$menuitems = $this->findAll("belongs_to = '$belongs_id'", '',
'Menu.zwaarte ASC');
foreach($menuitems as $item) {
$item = $item['Menu'];
if ($item['url'] == '') {
$submenu = $this->maakMenu($item['id']);
$menu_voor_view[$item['naam']] = $submenu;
} else if ($item['sub'] == '1') {
$submenu = $this->maakMenu($item['id']);
$menu_voor_view[$item['naam']] = $item['url'];
$menu_voor_view[$item['naam'].'.sub'] = $submenu;
} else {
$menu_voor_view[$item['naam']] = $item['url'];
}
}
return $menu_voor_view;
}
}$menu_voor_view = array to return
naam = link name
submenu = int to indicate if this menu has submenu's or not, so as to
allow a topcategory to have a url and subitems (it normally assumes
that if no url is there, it must have a submenu)
belongs_to = id of topcategory
zwaarte = shows the location of a specific menu in the list of allController:
class MenuController extends AppController {
var $name = 'Menu';function maakMenu($belongs_to = '0') {
if (isset($this->params['requested'])) {
$menu = $this->Menu->maakMenu($belongs_to);
return $menu;
} else {
$this->redirect('/');
}
}
}this just returns the menu array made in the model (and as it is
recursive there, it returns a complete menu array)To include a menu on a view, I created an element and that is
rendered:
$menu = $this->requestAction('menu/maakMenu');function printSubMenu($submenu, $html, $eerste) {
print '<ul';
if ($eerste) {
print ' id="nav"';
}
print '>';foreach($submenu as $naam => $url) {
// checking for submenu
if (!ereg('.*\.sub', $naam)) {
print '<li>';
if (is_array($url)) {
echo $html->link($naam, '#');
printSubMenu($url, $html, false);
} else if (isset($submenu[$naam.'.sub'])) {
echo $html->link($naam, '#');
printSubMenu($submenu[$naam.'.sub'], $html, false);
} else {
echo $html->link($naam, $url);
}
print '</li>';
}
}
}// $html is needed to use the $html helper functions!
printSubMenu($menu, $html, true);
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home