[eluser]wr5aw[/eluser]
It might be easier to setup your menu items in a config file. That way you can modify menu items without mucking around with the controller code.
Consider the following config file in config/menu.php:
Code:
<?php
// Company Menu
$config['menus']['company']['title'] = 'Company Title';
//
$config['menus']['company']['items']['somethin']['text'] = 'Somethin';
$config['menus']['company']['items']['somethin']['href'] = '/webcompany/index.php/topmenu/';
//
$config['menus']['company']['items']['somethin_new']['text'] = 'Somethin new';
$config['menus']['company']['items']['somethin_new']['href'] = '/webcompany/index.php/topmenu/aaa';
//
$config['menus']['company']['items']['somethin_else']['text'] = 'Somethin else';
$config['menus']['company']['items']['somethin_else']['href'] = '/webcompany/index.php/topmenu/bbb';
//
//
//Places Menu
$config['menus']['places']['title'] = 'Places Title';
//
$config['menus']['places']['items']['someplace']['text'] = 'Places';
$config['menus']['places']['items']['someplace']['href'] = '/webcompany/index.php/places/';
//
$config['menus']['places']['items']['someplace']['text'] = 'Someplace';
$config['menus']['places']['items']['someplace']['href'] = '/webcompany/index.php/places/aaa';
//
$config['menus']['places']['items']['someplace']['text'] = 'Someplace';
$config['menus']['places']['items']['someplace']['href'] = '/webcompany/index.php/places/aaa';
Then the menu template in views/menu.php:
Code:
<div class = "menu">
<?php
foreach ($menus as $menu_id=>$menu)
{
echo "\t\t<h3 id='$menu_id'>".$menu['title']."</h3>\n";
echo "\t\t<ul id='$menu_id'>\n";
foreach ($menu['items'] as $item_id=>$item)
{
echo "\t\t\t<li id='$item_id'>" . "<a href='" . $item['>" . $item['text'] . "</a></li>\n";
}
echo "\t\t</ul>\n";
}
?>
</div>
And a main template file in views/main.php:
Code:
<html>
<head>
<title>Testing Menus</title>
</head>
<body>
<div class="menus">
<?php echo $menus; ?>
</div>
<div class="content">
<?php echo $content; ?>
</div>
</body>
</html>
And finally, the controller welcome.php:
Code:
<?php
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->config('menus');
$data['menus'] = $this->config->item('menus');
$data['menus'] = $this->load->view('menu', $data, TRUE);
$data['content'] = "Your main content here";
$this->load->view('main', $data);
}
}
Here's the generated html:
Code:
<html>
<head>
<title>Testing Menus</title>
</head>
<body>
<div class="menus">
<div class = "menu">
<h3 id='company'>Company Title</h3>
<ul id='company'>
<li id='somethin'><a href='/webcompany/index.php/topmenu/'>Somethin</a></li>
<li id='somethin_new'><a href='/webcompany/index.php/topmenu/aaa'>Somethin new</a></li>
<li id='somethin_else'><a href='/webcompany/index.php/topmenu/bbb'>Somethin else</a></li>
</ul>
<h3 id='places'>Places Title</h3>
<ul id='places'>
<li id='someplace'><a href='/webcompany/index.php/places/aaa'>Someplace</a></li>
</ul>
</div>
</div>
<div class="content">
Your main content here</div>
</body>
</html>
As you can see, there are a couple of advantages to this method. First, you only have to edit config/menus.php to edit menu entries. Second, it wouldn't be too difficult to port the menu to database entries instead of a config file. And finally, since we're using array keys, which are unique, we can take advantage of that by using them as id params in our html which makes it very easy to style our output using css.