0

Needed Navigation Html

  • Home
  • Pages
    • About
    • Services
    • Products
    • Contact
    • FAQs
    • Sitemap
    • Privacy Policy
    • Column Layouts
      • 1 Column
      • 2 Column (Left Sidebar)
      • 2 Column (Right Sidebar)
      • 3 Column
      • 4 Column
  • I want to use php arrays and foreach loops to output the needed html. The php code I have thus far is:

    <?php
        $data = array("navigation");
        $data['navigation']['Home'] = base_url();
        $data['navigation']['Pages'] = base_url('pages');
        $data['navigation']['Pages']['About'] = base_url('pages/about');
        echo '<ul>';
        foreach($data as $nav) {
            foreach($nav as $subNavKey => $subNavHref) {
                echo "<li><a href='$subNavHref'>$subNavKey</a>";
            }
        }
        echo '</ul>';
    ?>
    

    I was thinking I would need three foreach loops nested but php warnings/errors are generated when the third loop is reached on lines such as:

    $data['navigation']['Home'] = base_url();
    $data['navigation']['Pages'] = base_url('pages');
    

    I'm not quite sure how to test for 3rd level depths such as:

    $data['navigation']['Pages']['About'] = base_url('pages/about');
    

    Also, outputting the needed li and ul tags in the proper positions has given me trouble aswell.

    4
    • What is the max depth you can have? Commented Apr 3, 2014 at 21:22
    • 2
      use recursive function, its better Commented Apr 3, 2014 at 21:22
    • Max depth is as deep as "1 Column" in above html Commented Apr 3, 2014 at 21:26
    • array("navigation"); Creates an array with one element, the string "navigation". I think you want $data = array("navigation" => array()); Commented Apr 3, 2014 at 21:26

    4 Answers 4

    4

    Use recursion

    $data['navigation']['Home'] = base_url();
    $data['navigation']['Pages'] = base_url('pages');
    $data['navigation']['Pages']['About'] = base_url('pages/about');
    $data['navigation']['Pages']['About']['Team'] = base_url('pages/team');
    $data['navigation']['Pages']['About']['Team']['Nate'] = base_url('pages/nate');
    
    echo "<ul>"
    print_list($data);
    echo "</ul>"
    
    function print_list($menu) {
      foreach($menu as $key=>$item) {
        echo "<li>";
        if(is_array($item)) {
          echo "<ul>";
          print_list($item);
          echo "</ul>";
        } else {
          echo "<a href='{$val}'>$key</a>";
        }
        echo "</li>";
      }
    }
    
    Sign up to request clarification or add additional context in comments.

    Comments

    2
    <?php
    
    function nav($data) {
        $html = '<ul>';
        foreach ($data as $k => $v) {
            if (is_array($v)) {
                $html .=  "<li>$k" . nav($v) . "</li>";
            }
            else {
                $html .= "<li><a href='$k'>$v</a>";
            }
        }
        $html .= '</ul>';
        return $html;
    }
    
    echo nav($data);
    

    1 Comment

    You should change $html .= nav($v); to $html .= "<li>$k". nav($v) ."</li>";, as you're loosing Pages and Column Layouts
    1

    A recursive function can get the job done:

    $items = array(
        "Home",
        "Pages" => array(
            "About",
            "Services",
            "Products",
            "Contact",
            "FAQs",
            "Sitemap",
            "Privacy Policy",
            "Column Layouts" => array(
                "1 Column",
                "2 Column (Left Sidebar)",
                "2 Column (Right Sidebar)",
                "3 Column",
                "4 Column"
            )
        )
    );
    
    function getMenu($array) {
        foreach($array as $key => $value) {
            if(is_array($value)) {
                echo "<li>" . $key . "</li>";
                echo "<ul>";
                getMenu($value);
                echo "</ul>";
            } else {
                echo "<li>" . $value . "</li>";
            }
        }
    }
    echo "<ul>";
    getMenu($items);
    echo "</ul>";
    

    Output:

    enter image description here

    2 Comments

    You got a bug, as you're printing your menu items outside main <ul> tags, like that <ul></ul><li>Home</li><li>Pages</li>....
    You still got a bug, as your child <ul> is outside parent tag <li>. You have to change echo "<li>" . $key . "</li>"; echo "<ul>"; getMenu($value); echo "</ul>"; to echo "<li>" . $key; echo "<ul>"; getMenu($value); echo "</ul></li>";
    1

    You should use a recursive function, for example (Working Demo):

    function makeMenu($array)
    {
        $menu = '';
        foreach($array as $key => $value) {
            if(is_array($value)) {
                $menu .= '<li>' . $key . '<ul>' . makeMenu($value) . '</ul></li>';
            }
            else {
                $menu .= "<li><a href='". $value ."'>" . $value ."</a></li>";
            }
        }
        return $menu;
    }
    

    Then call it like:

    $data = array(
        "Home",
        "Pages" => array("About", "Services"),
        "Column Layouts" => array("1 Column", "2 Column (Left Sidebar)")
    );
    echo '<ul>' . makeMenu($data) . '</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.