1

Hey I have question about Dynamic menu which is created in php. Code is from stackoverflow, what I want is to get my parent styled with red color if children of those parent is selected, here is code:

$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);

So how it needs to go:

Home
Parent - selected
Sub 1 - selected
Sub 2

or

Home
Parent - selected
Sub 1
Sub 2 - selected

Hope someone understand what i want? If my children under parent is selected also parent needs to be selected.

6
  • give the style to anchor instead of li Commented Feb 6, 2014 at 9:49
  • that makes no difference. Commented Feb 6, 2014 at 9:52
  • What is the content of $_GET['menu']? Commented Feb 6, 2014 at 9:53
  • I get link which page is opened with $_GET['menu'] Commented Feb 6, 2014 at 9:55
  • So $_GET['menu'] would contain values like a,b,c,d right? Commented Feb 6, 2014 at 10:01

4 Answers 4

1

I have added one function to check element in children array. May be something better solution are there. But at this its quick solution for you :)

$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

Sign up to request clarification or add additional context in comments.

7 Comments

Still doesnt select my parent of my childrens.
Ahh. I see . Let say if d is $_GET['menu'] value then d should display in red color and its parent as well. right?
Yes if Sub2 is selected than Parent needs to be also selected, but is not.
@user3278890 . Check my current solutions
Now is working but only on item D on item C doesnt select Parent and also if u use only parent doesnt select Parent
|
0

Use jQuery to add parent li a background color

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

1 Comment

I would like to add class to li throught php if its possible somehow.
0

Pass every menu level to the url. You can add the "selected" class for every menu level. So:

$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;

When building your menu, compare the 'to be build item' to the $current_menu_level1/2 variable and add echo the class when they are the same.

Comments

0

please, consider create a css class selected for <li> elements and <ul> elements.

With PHP insert this styles when needed, Like this:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.