0

I'm attempting to take the following array and create what is the same version of my static HTML version of my navigation. I added the HTML to show what it looks like for a category to not have any children and be by itself and how it looks when a category has children items. I've started but am now trying to figure out with the hasSubmenu.

Database Query Result

array(3) {
    [0]=> object(stdClass)#24 (3) {
        ["category_id"]=> string(1) "1"
        ["category_name"]=> string(9) "Dashboard"
        ["category_class"]=> string(9) "dashboard"
    }
    [1]=> object(stdClass)#24 (3) {
        ["category_id"]=> string(1) "2"
        ["category_name"]=> string(5) "Users"
        ["category_class"]=> string(5) "users"
    }
    [2]=> object(stdClass)#24 (3) {
        ["category_id"]=> string(1) "3"
        ["category_name"]=> string(5) "Pages"
        ["category_class"]=> string(5) "pages"
        ["links"]=> array(2) {
            [0]=> object(stdClass)#24 (2) {
                ["item_id"]=> string(1) "1"
                ["item_name"]=> string(5) "Admin Pages"
            [1]=> object(stdClass)#24 (2) {
                ["item_id"]=> string(1) "2"
                ["item_name"]=> string(5) "User Pages"
            }
        }
    }
}

HTML Generated Navigation

<li class="active"><a href="" class="glyphicons dashboard"><i></i><span>Dashboard</span></a></li>
<li class="hasSubmenu">
    <a href="#menu_pages" data-toggle="collapse" class="glyphicons page"><i></i><span>Pages</span><span class="icon-chevron-down"></span></a>
    <ul class="collapse" id="menu_pages">
        <!-- Components Submenu Regular Items -->
        <li class=""><a href="">Item 1</a></li>
        <li class=""><a href="">Item 2</a></li>
        <li class=""><a href="">Item 3</a></li>
        <li class=""><a href="">Item 4</a></li>
        <!-- // Components Submenu Regular Items END -->
    </ul>
</li>

PHP Started Navigation

foreach ($dashboard_menu_categories AS $category)
{
    echo '<li>';
    if (isset($category->links))
    {
        echo '<ul class="collapse" id="menu_'.$category->category_short_name.'">';
        /* Components Submenu Regular Items */
        foreach($category->links AS $item)
        {
            echo '<li class=""><a href="">'.$item->item_name.'</a></li>';
        }
        /* Components Submenu Regular Items END */
        echo '</ul>';
    }
    echo '<a href="" class="glyphicons '.$category->category_class.'"><i></i><span>'.$category->category_name.'</span></a>';
    echo '</li>';
}
3
  • What is the specific problem you've encountered? Commented Nov 17, 2013 at 6:09
  • Trying to get my php to match my HTML Commented Nov 17, 2013 at 6:36
  • What is the specific problem you've encountered? Commented Nov 17, 2013 at 7:01

1 Answer 1

0

You just need to use conditionals for the classes/attributes that are added to the categories with submenus:

foreach ($dashboard_menu_categories AS $category)
{
    $hasSubmenu = isset($category->links);

    echo '<li class="'.($hasSubmenu ? 'hasSubmenu' : '').'">';
    echo '<a href=""'.($hasSubmenu ? ' data-toggle="collapse"' : '').' class="glyphicons '.$category->category_class.'"><i></i><span>'.$category->category_name.'</span>';
    if($hasSubmenu)
        echo '<span class="icon-chevron-down"></span>';
    echo '</a>';
    if ($hasSubmenu)
    {
        echo '<ul class="collapse" id="menu_'.$category->category_short_name.'">';
        /* Components Submenu Regular Items */
        foreach($category->links AS $item)
        {
            echo '<li class=""><a href="">'.$item->item_name.'</a></li>';
        }
        /* Components Submenu Regular Items END */
        echo '</ul>';
    }
    echo '</li>';
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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