Using recursion to build navigation

后端 未结 2 1604
抹茶落季
抹茶落季 2020-12-15 01:35

I\'m building navigation for a site and for the life of me I can\'t figure out recursion. I have all my data stored via MySQL using this design:

相关标签:
2条回答
  • 2020-12-15 02:09

    I have the same above code with little bit modification, so that a user can apply different css on each level of menu, now its showing classes of sub menu like child-class1 when its in 1st sub menu , and it will show child-class2 when its in 2nd sub menu and so on...

      <?php
     function buildNavigation($items, $parent = NULL, $n=NULL)
        {
    $hasChildren = false;
    if ($parent == NULL)
    {
        $level=0;
        $outputHtml = '<ul class="parent-class">%s</ul>';
    }
    else
    {
        if($n==NULL)
        {
            $level=1;
        }
        else
        {
            $level=$n;
        }
        $outputHtml = '<ul class="child-class'.$level.'">%s</ul>';  
    } 
    $childrenHtml = '';
    foreach($items as $item)
    {
        if ($item['parent'] == $parent) {
            $hasChildren = true;
            $childrenHtml .= '<li><a href="/'.$item['slug'].'">'.$item['ptitle'].'</a>';
            $next = ++$level;
            $childrenHtml .= buildNavigation($items, $item['pageid'],$next);         
            $childrenHtml .= '</li>';           
        }
    }
    
    // Without children, we do not need the <ul> tag.
    if (!$hasChildren) {
        $outputHtml = '';
    }
    
    // Returns the HTML
    return sprintf($outputHtml, $childrenHtml);
    }
    echo  buildNavigation($ppages);
    ?>
    

    it will show out put like this

        <ul class="parent-class">
          <li>
            <a href="http://example.com/page-1">page 1</a>
               <ul class="child-class1">
                  <li>
                     <a href="http://example.com/this-is-child-page">this is child page</a>
                      <ul class="child-class2">
                         <li>
                            <a href="http://example.com/child-of-child">child of child</a>                 </li>
                      </ul>
                  </li>
              </ul>
          </li>
     </ul>
    

    I would like to thanks Mr @Maxime Morin.

    0 讨论(0)
  • 2020-12-15 02:11

    Here's an example with recursion.

    function buildNavigation($items, $parent = NULL)
    {
        $hasChildren = false;
        $outputHtml = '<ul>%s</ul>';
        $childrenHtml = '';
    
        foreach($items as $item)
        {
            if ($item['parent'] == $parent) {
                $hasChildren = true;
                $childrenHtml .= '<li>'.$item['category_name'];         
                $childrenHtml .= buildNavigation($items, $item['category_id']);         
                $childrenHtml .= '</li>';           
            }
        }
    
        // Without children, we do not need the <ul> tag.
        if (!$hasChildren) {
            $outputHtml = '';
        }
    
        // Returns the HTML
        return sprintf($outputHtml, $childrenHtml);
    }
    
    print buildNavigation($items);
    

    That script produces the following output :

    <ul>
        <li>Menu 1</li>
        <li>Menu 2
            <ul>
                <li>Sub Menu 2.1</li>
                <li>Sub Menu 2.2</li>
                <li>Sub Menu 2.3
                    <ul>
                        <li>Sub Menu 2.2.1</li>
                        <li>Sub Menu 2.2.2</li>
                        <li>Sub Menu 2.2.3</li>
                    </ul>
                </li>
            </ul>
        </li>
        <li>Menu 3</li>
    </ul>
    
    0 讨论(0)
提交回复
热议问题