从动态二维数组创建多级菜单

时间:2014-10-07 22:12:30

标签: php mysql arrays codeigniter

我正在尝试从mysql表和php的以下输出创建多级菜单。

这是我的代码:

    $data['menu_response'] = $this->mmenus->getAllMenus($mem_id);

    $array = array();
    $sub_array = array();

    foreach ($data['menu_response'] as $v1) {
        $array[] = $v1->MenuName;
        $sub_array[] = $v1->SubMenuName;
        //print_r($v1->SubMenuName . "</br>");
        //print_r($v1->MenuName . "</br>");
    }

    print_r($array);
    print_r($sub_array);

我将打印输出作为

Array ( [0] => News [1] => News [2] => News [3] => Video [4] => Video [5] => Audio ) Array ( [0] => All [1] => Summary [2] => Preview [3] => Summary [4] => Preview [5] => Summary )

这里第一个数组是父数组,第二个数组是子数组。

db查询是:

  

SELECT prm_members_menus.id, prm_linked_menus.menuLinkedID,prm_linked_menus.menuID, prm_linked_menus.SubMenuID, (SELECT prm_menus.name from prm_menus where prm_menus.id = prm_linked_menus.menuID) as MenuName , (SELECT prm_sub_menus.name from prm_sub_menus where prm_sub_menus.subMenuID = prm_linked_menus.subMenuID) as SubMenuName from prm_members_menus inner join prm_linked_menus on prm_linked_menus.menuLinkedID = prm_members_menus.menuLinkedID WHERE prm_members_menus.mem_id=2 order by prm_members_menus.id

如何从普通父级的上述输出生成多级菜单?

我正在为我的网络应用程序为不同类型的用户构建动态导航菜单。 我想显示

的导航菜单
**MEMBER_ID 1:**

NEWS

    -- ALL

    -- PREVIEW

    -- SUMMARY

AUDIO

    -- PREVIEW 

VIDEO

    -- ALL

**MEMBER_ID 2:**

NEWS

    -- ALL

    -- PREVIEW

AUDIO

    -- PREVIEW 

VIDEO

    -- ALL



**MEMBER_ID 3:**

AUDIO

    -- PREVIEW 

VIDEO

    -- ALL

我一直在尝试使用上述数据库表结构填充MULTI-LEVEL NAVIGATION MENU LIST几个小时。我正在尝试填充动态菜单列表,它是使用FOREACH LOOP的href,icon,并已在codeigniter和mysql中编码。

Darren帮助生成了父子菜单列表。

现在,下一个问题是从返回的查询中动态更改Menu,SubMenu href,图标以及MenuName,SubMenuName。如何实现?

&LT;&LT;&LT;

mysql查询的PHP输出:

  Array ( [0] => stdClass Object ( [id] => 10 [menuLinkedID] => 1 [menuID] => 2 [SubMenuID] => 1 [MenuName] => News [SubMenuName] => All ) [1] => stdClass Object ( [id] => 11 [menuLinkedID] => 2 [menuID] => 2 [SubMenuID] => 2 [MenuName] => News [SubMenuName] => Summary ) [2] => stdClass Object ( [id] => 12 [menuLinkedID] => 3 [menuID] => 2 [SubMenuID] => 3 [MenuName] => News [SubMenuName] => Preview ) [3] => stdClass Object ( [id] => 14 [menuLinkedID] => 5 [menuID] => 3 [SubMenuID] => 2 [MenuName] => Video [SubMenuName] => Summary ) [4] => stdClass Object ( [id] => 15 [menuLinkedID] => 6 [menuID] => 3 [SubMenuID] => 3 [MenuName] => Video [SubMenuName] => Preview ) [5] => stdClass Object ( [id] => 17 [menuLinkedID] => 8 [menuID] => 4 [SubMenuID] => 2 [MenuName] => Audio [SubMenuName] => Summary ) )

查询的UPDATED PHP输出以获取MenuName,SubMenuName,href,icon:

查询: ...............

  

SELECT prm_members_menus.id, prm_linked_menus.menuLinkedID,prm_linked_menus.menuID, prm_linked_menus.SubMenuID, prm_menus.href as parent_href,prm_menus.i_class as parent_i_class,prm_sub_menus.href as child_href,prm_sub_menus.i_class as child_i_class, (SELECT prm_menus.name from prm_menus where prm_menus.id = prm_linked_menus.menuID) as MenuName , (SELECT prm_sub_menus.name from prm_sub_menus where prm_sub_menus.subMenuID = prm_linked_menus.subMenuID) as SubMenuName from prm_members_menus inner join prm_linked_menus on prm_linked_menus.menuLinkedID = prm_members_menus.menuLinkedID inner join prm_menus on prm_menus.id = prm_linked_menus.menuID inner join prm_sub_menus on prm_sub_menus.subMenuID = prm_linked_menus.subMenuID WHERE prm_members_menus.mem_id= 2 order by prm_members_menus.id

输出是: ..............

  Array ( [0] => stdClass Object ( [id] => 1 [menuLinkedID] => 1 [menuID] => 2 [SubMenuID] => 1 [parent_href] => news [parent_i_class] => fa fa-list-alt [child_href] => all [child_i_class] => fa fa-folder-o [MenuName] => News [SubMenuName] => All ) [1] => stdClass Object ( [id] => 2 [menuLinkedID] => 2 [menuID] => 2 [SubMenuID] => 2 [parent_href] => news [parent_i_class] => fa fa-list-alt [child_href] => summary [child_i_class] => fa fa-folder-o [MenuName] => News [SubMenuName] => Summary ) [2] => stdClass Object ( [id] => 3 [menuLinkedID] => 3 [menuID] => 2 [SubMenuID] => 3 [parent_href] => news [parent_i_class] => fa fa-list-alt [child_href] => preview [child_i_class] => fa fa-folder-o [MenuName] => News [SubMenuName] => Preview ) [3] => stdClass Object ( [id] => 4 [menuLinkedID] => 4 [menuID] => 3 [SubMenuID] => 1 [parent_href] => video [parent_i_class] => fa fa-eye [child_href] => all [child_i_class] => fa fa-folder-o [MenuName] => Video [SubMenuName] => All ) [4] => stdClass Object ( [id] => 5 [menuLinkedID] => 6 [menuID] => 3 [SubMenuID] => 3 [parent_href] => video [parent_i_class] => fa fa-eye [child_href] => preview [child_i_class] => fa fa-folder-o [MenuName] => Video [SubMenuName] => Preview ) [5] => stdClass Object ( [id] => 6 [menuLinkedID] => 7 [menuID] => 4 [SubMenuID] => 1 [parent_href] => audio [parent_i_class] => fa fa-folder-o [child_href] => all [child_i_class] => fa fa-folder-o [MenuName] => Audio [SubMenuName] => All ) [6] => stdClass Object ( [id] => 7 [menuLinkedID] => 8 [menuID] => 4 [SubMenuID] => 2 [parent_href] => audio [parent_i_class] => fa fa-folder-o [child_href] => summary [child_i_class] => fa fa-folder-o [MenuName] => Audio [SubMenuName] => Summary ) [7] => stdClass Object ( [id] => 8 [menuLinkedID] => 9 [menuID] => 4 [SubMenuID] => 3 [parent_href] => audio [parent_i_class] => fa fa-folder-o [child_href] => preview [child_i_class] => fa fa-folder-o [MenuName] => Audio [SubMenuName] => Preview ) )

2 个答案:

答案 0 :(得分:1)

这对你有用吗?

不知道是否需要创建一个以后要打印的多维数组(也就是$ array和$ sub_array的原因),或者只是需要显示它。我的假设基于你的问题,你只需要显示它,这对你来说是一个很好的答案。

$MenuName = "";
foreach ($data['menu_response'] as $v1) {
    if ($v1->MenuName != $MenuName) {
        // New Main Menu Line
        if ($MenuName != "") {
            echo "</ul>";
        }
        echo "<ul>";
        $MenuName = $v1->MenuName;
        print_main_menu($v1->MenuName);
    }
    print_sub_main_menu($v1->SubMenuName, $v1->menuLinkedID);
}
if ($MenuName != "") {
    echo "</ul>";

print_main_menu($Text) {
    echo "<li>$Text</li>";
}

print_sub_menu($Text, $MenuId) {
    echo "<li><a href=$MenuId>$Text</a></li>";
}

编辑以显示如何将HTML附加到项目的示例。

答案 1 :(得分:1)

我不明白为什么你要分开父级菜单和子菜单,你怎么知道哪个子菜单属于哪个菜单?

您应该将菜单更改为以下内容:

$menu = array();
foreach ($data['menu_response'] as $v1) {
    $menu[$v1->MenuName][] = $v1->SubMenuName;
}

从技术上讲,这会给你类似的东西:

Array (
    [Menu-item] => Array(
        [0] => [Sub-menu-item],
        [2] => [Sub-menu-item],
    )
.....etc
)

这样你就可以简单地遍历$menu数组并将其打印出你想要的结构。

示例:

echo "<ul>";
foreach($menu as $title => $submenu) {
    echo "<li>";
    echo $title;
    if(!empty($submenu)) {
        echo "<ul>";
        foreach($submenu as $item) {
            echo "<li>{$item}</li>";
        }
        echo "</ul>";
    }
    echo "<li>";
}
echo "</ul>";

更新的答案: 你只需创建一个像我在评论中所述的关联数组:) 像这样的简单循环应该:

$menu = array();
foreach ($data['menu_response'] as $v1) {
    $menu[$v1->MenuName][] = array('submenu' => $v1->SubMenuName, 'icon' => $v1->child_i_class);
}

你可以像这样打印出来:

echo "<ul>";
foreach($menu as $title => $submenu) {
    echo "<li>";
    echo $title;
    if(!empty($submenu)) {
        echo "<ul>";
        foreach($submenu as $item) {
            echo "<li><i class='{$item['icon']}'></i> {$item['submenu']}</li>";
        }
        echo "</ul>";
    }
    echo "<li>";
}
echo "</ul>";