[eluser]Mirage[/eluser]
Hi -
Not certain if you're just doing this for testing purposes, but you wouldn't make a specific controller for building a menu.
You're essentially on the right path, though it seems like you have a bit of trouble with passing things around and OO practices in general.
Controller
Code:
class SomeController extends Controller {
// Contsructor
function SomeController() {
parent::Controller();
// we always need the menu, so we can load that model here
$this->load->model('MenuModel','',true);
}
// show some page
function index() {
$menu=$this->MenuModel->buildMenu();
$this->load->view('index_view',compact('menu');
}
}
Model
Code:
class MenuModel extends Model {
// Contsructor
function MenuModel() {
parent::Model();
}
// build the entire menu as a nested array
function buildMenu() {
$menu=$this->getMainMenu();
if(!is_array($menu)) {
return array();
}
foreach($menu as $idx=>$name) {
$menu[$idx]['submenu']=$this->getSubMenu($name);
}
return $menu;
}
// grab the top level menu
function getMainMenu() {
$this->db->select('main_menu as name');
$this->db->group_by('main_menu');
$query = $this->db->get('inbook_links');
return ($query && $query->num_rows()>0 ? $query->result_array() : array());
}
// grab a submenu given a parent menu
function getSubMenu($parent_menu) {
$this->db->select('sub_menu as name');
$this->db->where('main_menu', $parent_menu);
$query=$this->db->get('inbook_links);
return ($query && $query->num_rows()>0 ? $query->result_array() : array());
}
}
View
Code:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1” />
<title>Untitled Document</title>
</head>
<body>
<div class="mC">
<?php foreach($menu as $topId=>$topMenu): ?>
<div class="mH" onmouseover="toggleMenu(’<?php echo "menu{$topId}" ?>’)"><?php echo $topMenu['name']?></div>
<?php if(!empty($topMenu['submenu'])):?>
<div id="<?php echo "menu{$topId}" ?>” class="mL" style="display:none;">
<?php foreach($topMenu['submenu'] as $subId=> $subMenu): ?>
<div class="mH"><?php echo $subMenu['name']?></div>
<?php endforeach;?>
<?php endif;?>
<?php endforeach;?>
</div>
</body>
</html>
Code is not tested, but I hope this gets you on your way.
Good luck!
m