Cakephp creating dynamic menus
Generating categories or list of selectable items in cake sites are common task this can be done by pulling list of category data in your app controller and then using one view element so that they appear in every page. Here is the step by step process to do that. Add the following lines of code in your app_controller.php file.
$categories = ClassRegistry::init('Category')->find('all');
$this->set('categories', $categories);
Above lines of code can be put under beforeRender() function in app_controller.php I am assuming that categories is the name of table. Next thing is to create one element file in views/elements/ folderĀ with any name say category.ctp now place the following code under that
<ul> <?php foreach($categories as $key => $data) : ?> <li><?php echo $data['Category']['name'] ?></li> <?php endforeach; ?> <ul>
The last thing to actually show this output into your your website you need to edit current layout file (default.ctp etc) and include your element that created in previous step. just all the following line of code in your layout file
<?php echo $this->element('category'); ?>
Thats all… Post a comment here if you still face any problem.
Thank you very much this information was usefull and worked for me. Saludos!