Menus, Submenus in Codeigniter with Boostrap
http://forums.phpfreaks.com/topic/207853-php-codeigniter-dynamic-navigation/
<?php
// ######################## MODEL
/*=================================================================================
GET MENU ITEMS
==================================================================================*/
function MenuSub(){
// get all parent menus
$this->db->select('*');
$this->db->where('parent_id', 0);
$query = $this->db->get('pages');
$i = 0;
$sub = 0;
foreach($query->result() as $row) {
$parentitem[$i]['id'] = $row->id;
$parentitem[$i]['title'] = $row->title;
$parentitem[$i]['slug'] = $row->slug;
$parentitem[$i]['is_child'] = $row->is_child;
$parentitem[$i]['parent_id'] = $row->parent_id;
// get Sub Menus based on parent page_id above, which will be the same as parent_id
$this->db->select('*');
$this->db->where('parent_id', $parentitem[$i]['id']);
$subquery = $this->db->get('pages');
// check if subquery was successfull, this will prevent undefined variables is subpages dont exist
if( empty($subquery) ) {
// at this point we dont have any subpages for this iteration, set these arrays to empty
$subitem = array();
$subparentitem = array() ;
}
else {
// loop through this collection and store all subpages for this page_id
// here we have subpages
foreach($subquery->result() as $row) {
$subitem[$sub]['id'] = $row->id;
$subitem[$sub]['parent_id'] = $row->parent_id;
$subitem[$sub]['title'] = $row->title;
$subitem[$sub]['slug'] = $row->slug;
$parentitem[$i]['is_child'] = $row->is_child;
$subparentitem[$sub]['parent_id'] = $row->parent_id;
$sub++;
} // end submenus foreach
}
$i++; // go to the next iteration
} // end main foreach
// default menu
$nav = array();
$nav['parent'] = $parentitem;
// check if these 2 arrays are not empty
if( !empty($subitem) && !empty($subparentitem) ) {
$nav['sub'] = $subitem;
$nav['subparent'] = $subparentitem;
}
return $nav;
}
// ######################## CONTROLLER
public function index () { // Default View on SITE
// GET ARTICLES
$data['articles'] = $this->Article_model->get_articles('id', 'DESC', '10');
// GET MENU ITEMS
$data['menu_items'] = $this->Page_model->get_menu_items() ;
$data['all_menu_items'] = $this->Page_model->get_all_menu_items() ;
$data['nav'] = $this->Page_model->MenuSub();
// Load View
//$this->load->view('homepage', $data) ;
$this->load->view('home', $data) ;
}
// ######################## VIEW
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="<?php echo base_url() ; ?>">Home</a></li>
<?php foreach($nav['parent'] as $parent) : ?>
<?php // if nav sub is not empty , this is a submenu
if( $parent['is_child'] == 1 ) : ?>
<li class="dropdown" >
<a href="<?php echo base_url() . 'pages/view_by_slug/' . $parent['slug']; ?>" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<?php echo $parent['title'] ; ?><span class="caret"></span></a>
<ul class="dropdown-menu">
<?php foreach($nav['sub'] as $subitem) {
if($parent['id'] == $subitem['parent_id'] ) { ?>
<li><a href="<?php echo base_url() . 'pages/view_by_slug/' . $subitem['slug']; ?>"><?php echo $subitem['title'] ; ?></a></li>
<?php }
}
?>
</ul>
</li>
<?php else : // no subpages, this is a normal page ?>
<li>
<a href="<?php echo base_url() . 'pages/view_by_slug/' . $parent['slug']; ?>">
<?php echo $parent['title'] ; ?></a>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div><!--/.nav-collapse -->
PAGES table:
+------------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | | NULL | |
| slug | varchar(300) | NO | | NULL | |
| meta_description | varchar(400) | NO | | NULL | |
| body | text | NO | | NULL | |
| access | int(4) | NO | | 0 | |
| parent_id | tinyint(4) | NO | | NULL | |
| is_child | tinyint(1) | NO | | 0 | |
| is_published | tinyint(1) | NO | | 1 | |
| in_menu | tinyint(1) | NO | | 0 | |
| order | int(11) | NO | | NULL | |
| created | timestamp | NO | | CURRENT_TIMESTAMP | |
+------------------+--------------+------+-----+-------------------+----------------+