Array (
[0] => Array ( [menu_id] => 1 [menu_title] => Home [menu_link] => home [parent_menu] => 0 [menu_pos] => H [status] => Y )
[1] => Array ( [menu_id] => 2 [menu_title] => About [menu_link] => about [parent_menu] => home [menu_pos] => H [status] => Y )
[2] => Array ( [menu_id] => 4 [menu_title] => test1 [menu_link] => test1 [parent_menu] => about [menu_pos] => H [status] => Y )
[3] => Array ( [menu_id] => 5 [menu_title] => Help [menu_link] => help [parent_menu] => about [menu_pos] => H [status] => Y )
[4] => Array ( [menu_id] => 7 [menu_title] => test_new [menu_link] => test [parent_menu] => test [menu_pos] => H [status] => Y )
)
我想通过使用父菜单获取菜单链接,子链接.i打印数组请帮帮我。
public function menu ($type)
{
$pages = $this->ci->db->query("SELECT * FROM ".MENU." WHERE menu_pos='".$type."'")->result_array();
$array = $pages;
//print_r($pages);
return $this->build_menu($array,$parent_id = 0,$level=0);
//return $array;
}
function build_menu($array, $parent = 0, $level = 0)
{
$ret = '<ul>';
foreach($array as $m)
{
if($m['parent_menu'] == $parent)
{
$ret .= '<li>'.$m['menu_link'];
$ret .= $this->build_menu($array, $m['parent_menu'], $level + 1);
$ret .= '</li>';
}
}
return $ret.'</ul>';
print $ret;
}
我不知道为什么会发生这种情况。我的递归函数返回错误。
答案 0 :(得分:0)
主要错误是在你对函数build_menu的递归调用中传递了当前页面的父节点而不是当前页面。
~/Workspace/Nifty
我不知道为什么你也通过$ level,但也许需要你的东西。