jaapjolman Posted July 15, 2010 Share Posted July 15, 2010 Controller: <?php function gui() { $nav = $this->site_model->navigation(); $page = $this->site_model->page_config(); $this->load->view('site/site',array('page' => $page, 'nav'=> $nav)); } ?> Model: <?php function navigation() { // Parent Menu $this->db->order_by('order', 'asc'); $this->db->where('subid', '0'); $query = $this->db->get('navigation'); $i = 0; foreach($query->result() as $row) { $parentitem[$i]['id'] = $row->id; $parentitem[$i]['link'] = $row->link; $parentitem[$i]['title'] = $row->title; // Sub Menu $this->db->order_by('order', 'asc'); $this->db->where('subid', $parentitem[$i]['id']); $subquery = $this->db->get('navigation'); $sub = 0; foreach($subquery->result() as $row) { $subitem[$sub]['id'] = $row->id; $subitem[$sub]['link'] = $row->link; $subitem[$sub]['title'] = $row->title; $sub++; } $i++; } $nav; $nav = array(); $nav['parent'] = $parentitem; $nav['sub'] = $subitem; return $nav; } ?> View: <?php foreach($nav['parent'] as $parent) { echo '<h3><a name="'.$parent['link'].'">'.$parent['title'].'</a></h3>'; echo '<div>'; echo '<ul>'; foreach($nav['sub'] as $subitem) { echo '<li><a name="'.$subitem['link'].'">'.$subitem['title'].'</a></li>'; } echo '</ul>'; echo '</div>'; } ?> the script works for the parent navigational links but not the sub links it keeps repeating the last subitem from the db I only posted the relivant portion of code Quote Link to comment https://forums.phpfreaks.com/topic/207853-php-codeigniter-dynamic-navigation/ Share on other sites More sharing options...
jaapjolman Posted July 16, 2010 Author Share Posted July 16, 2010 can anyone please help me Quote Link to comment https://forums.phpfreaks.com/topic/207853-php-codeigniter-dynamic-navigation/#findComment-1086905 Share on other sites More sharing options...
anthrt Posted July 28, 2010 Share Posted July 28, 2010 Try putting $sub = 0; Outside of the loop Quote Link to comment https://forums.phpfreaks.com/topic/207853-php-codeigniter-dynamic-navigation/#findComment-1092369 Share on other sites More sharing options...
AshishMalaviya Posted September 24, 2015 Share Posted September 24, 2015 model : --> function MenuSub(){ $this->db->select('*'); $this->db->where('page_parent_id', '0'); $query = $this->db->get($this->table); $i = 0; $sub = 0; foreach($query->result() as $row) { $parentitem[$i]['page_id'] = $row->page_id; $parentitem[$i]['page_menutitle'] = $row->page_menutitle; $parentitem[$i]['page_parent_id'] = $row->page_parent_id; // Sub Menu $this->db->select('*'); $this->db->where('page_parent_id', $parentitem[$i]['page_id']); $subquery = $this->db->get($this->table); foreach($subquery->result() as $row) { $subitem[$sub]['page_id'] = $row->page_id; $subitem[$sub]['page_parent_id'] = $row->page_parent_id; $subitem[$sub]['page_menutitle'] = $row->page_menutitle; $subparentitem[$sub]['page_parent_id'] = $row->page_parent_id; $sub++; } $i++; } $nav; $nav = array(); $nav['parent'] = $parentitem; $nav['sub'] = $subitem; $nav['subparent'] = $subparentitem; return $nav; } Controller :--> function index(){ $data['nav'] = $this->page->MenuSub(); } view :--> <?php foreach($nav['parent'] as $parent) { echo '<li><a name="'.$parent['page_id'].'">'.$parent['page_menutitle'].'</a>'; echo '<div>'; echo '<ul>'; foreach($nav['sub'] as $subitem) { if($parent['page_id'] == $subitem['page_parent_id']){ echo '<li><a name="'.$subitem['page_parent_id'].'">'.$subitem['page_menutitle'].'</a></li>'; }else{} } echo '</ul>'; echo '</div>'; echo '</li>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/207853-php-codeigniter-dynamic-navigation/#findComment-1521459 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.