Using recursion to build navigation

后端 未结 2 1611
抹茶落季
抹茶落季 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...

      %s
'; } else { if($n==NULL) { $level=1; } else { $level=$n; } $outputHtml = '
    %s
'; } $childrenHtml = ''; foreach($items as $item) { if ($item['parent'] == $parent) { $hasChildren = true; $childrenHtml .= '
  • '.$item['ptitle'].''; $next = ++$level; $childrenHtml .= buildNavigation($items, $item['pageid'],$next); $childrenHtml .= '
  • '; } } // Without children, we do not need the
      tag. if (!$hasChildren) { $outputHtml = ''; } // Returns the HTML return sprintf($outputHtml, $childrenHtml); } echo buildNavigation($ppages); ?>

      it will show out put like this

          
      

      I would like to thanks Mr @Maxime Morin.

    提交回复
    热议问题