i have a menu generated dynamically by taking title from database. i have set same table for sub pages by adding a column p_id. and my code looks like this.
private $Section = array();
function _menu($root){
$this->db->select('id,title,p_id');
$this->db->Where('p_id',$root);
$this->db->Where('status','active');
$this->db->Where('type','page');
$this->db->order_by('sort');
$query = $this->db->get('pages');
foreach ($query->result() as $row)
{
$this->Section[] = '<ul>';
$this->Section[] = '<li>';
$this->Section[] = anchor('site/page/'.$row->id.'#cont_main',$row->title);
$this->_menu($row->id);
$this->Section[] = '</li>';
$this->Section[] = '</ul>';
}
$query->free_result();
return $this->Section;
}
//get menu tree
function MenuTree(){
return $this->_menu(0);
}
in above _menu() if a nav have two or more child then it doesn’t displays because the source populated by it gives extra </ul> and <ul>. the source looks like this
<ul>
<li><a href="#">menu 1</a>
<ul>
<li><a>menu 1 sub1</a></li>
</ul>
<ul>
<li><a>menu 1 sub2</a></li>
</ul>
</li>
while i am assuming to get:
<ul>
<li><a href="#">menu 1</a>
<ul>
<li><a>menu 1 sub1</a></li>
<li><a>menu 1 sub2</a></li>
</ul>
</li>
edited
i have to add menu according to child element. image comes in all menus which doesn’t have child too due to extra <ul></ul> if i use ul before loop. the source looks like this.
<ul>
<li><a href="#">menu 1</a>
<ul>
<li><a>menu 1 sub1</a>
<ul></ul>
</li>
<li><a>menu 1 sub2</a></li>
</ul>
</li>
any idea would be appreciable.
You need to add your
uloutside the foreach loop AND make sure that you already have results!