How to build unlimited level of menu through PHP and mysql

前端 未结 8 1521
心在旅途
心在旅途 2020-11-29 01:05

Well, to build my menu my menu I use a db similar structure like this

  2  Services                  0
  3  Photo Gallery             0
  4  Home                         


        
8条回答
  •  感动是毒
    2020-11-29 01:24

    i would use a recursive function.

    i know this isn't exactly like your code, but I think you can get the general concept if you understand recursion. if you don't understand recursion check out http://en.wikipedia.org/wiki/Recursion_(computer_science)

    $list = new List();
    
    function print_menu($list) {
    
        echo '
      '; foreach($list as $item) { echo '
    • ' . $item->name . ''; if($item->has_child) { print_menu($item); } echo '
    • '; } echo '
    '; }

提交回复
热议问题