PHP数组的动态菜单

时间:2014-02-06 09:42:36

标签: php arrays menu

嘿,我对在php中创建的动态菜单有疑问。 代码来自stackoverflow,我想要的是如果选择了那些父级的子代,我的父级样式为红色,这里是代码:

$menu = Array(
    Array(
        'title' => 'Home',
        'link' => 'a'
    ),
    Array(
        'title' => 'Parent',
        'link' => 'b',
        'children' => Array(
            Array(
                'title' => 'Sub 1',
                'link' => 'c'
            ),
            Array(
                'title' => 'Sub 2',
                'link' => 'd'
            ),
        )
    )
); 
function buildMenu($menuArray)
{
    foreach ($menuArray as $node)
    {
        $selected = ($node['link']== $_GET['menu']) ? $selected = 'style="color: red;"' : null;
        echo "<li ".$selected."><a href='?menu=".$node['link']."'/>" . $node['title'] . "</a>";
        if ( ! empty($node['children'])) {
            echo "<ul>";
            buildMenu($node['children']);
            echo "</ul>";
        }
        echo "</li>";
    }
}
buildMenu($menu);

所以它需要走的路:

主页
家长 - 选择了 Sub 1 - 选择
Sub 2

主页
家长 - 选择了 Sub 1
Sub 2 - 选中

希望有人明白我想要的东西?如果选择了我父母的子女,则也需要选择父母。

4 个答案:

答案 0 :(得分:1)

我添加了一个函数来检查children数组中的元素。可能是更好的解决方案。但在此,它的快速解决方案:)

$menu = array(
    array(
        'title' => 'Home',
        'link' => 'a'
    ),
    array(
        'title' => 'Parent',
        'link' => 'b',
        'children' => array(
            array(
                'title' => 'Sub 1',
                'link' => 'c'
            ),
            array(
                'title' => 'Sub 2',
                'link' => 'd'
            ),
        )
    )
);

function buildMenu($menuArray) {
    foreach ($menuArray as $node) {

        $getMenu = isset($_GET['menu']) ? $_GET['menu'] : '';
        $checkParent = (isset($node['children']) && !empty($node['children'])) ? checkInChildArray($getMenu, $node['children']) : '';
        $parentSelected = ($checkParent) ? $selected = 'style="color: red;"' : null;
        echo "<li " . $parentSelected . "><a href='?menu=" . $node['link'] . "'>" . $node['title'] . "</a></li>";
        if (isset($node['children']) && !empty($node['children'])) {

            echo "<ul>";
            foreach ($node['children'] as $subMenu) {
                $childSelected = ($subMenu['link'] == $getMenu) ? $selected = 'style="color: red;"' : null;
                echo "<li " . $childSelected . "><a href='?menu=" . $subMenu['link'] . "'>" . $subMenu['title'] . "</a></li>";
            }
            echo "</ul>";
        }
        echo "</li>";
    }
}

// Checking if selected menu inside children array. 

function checkInChildArray($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item['link'] === $needle : $item == $needle) || (is_array($item) && checkInChildArray($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}


echo buildMenu($menu);

Working Demo

答案 1 :(得分:0)

使用jQuery为父li添加背景颜色

$('li.selected').parent().closest('li').css("color","red"); 

答案 2 :(得分:0)

将每个菜单级别传递给网址。您可以为每个菜单级别添加“selected”类。所以:

$current_menu_level_1 = (isset($_GET['menu_level_1'])) ? $_GET['menu_level_1'] : false;
$current_menu_level_2 = (isset($_GET['menu_level_2'])) ? $_GET['menu_level_2'] : false;

构建菜单时,将'to build item'与$ current_menu_level1 / 2变量进行比较,并在类相同时添加echo。

答案 3 :(得分:0)

请考虑为<li>元素和<ul>元素创建一个css类。

使用PHP在需要时插入此样式,如下所示:

$selected = ($node['link']== $_GET['menu']) ? $selected = 'selected' : '';
echo "<li class='".$selected."'>";

echo "<ul class='".$selected."'>";
        buildMenu($node['children']);
echo "</ul>";
相关问题