PHP / MySQL build tree menu

后端 未结 3 859
夕颜
夕颜 2020-11-29 03:24

I am trying to build an un-oredered list menu tree from my database in PHP and MySQL.

I have an array of page objects I am returning from the db. Each page object

3条回答
  •  青春惊慌失措
    2020-11-29 03:52

    Instead of querying the database recursively, you can just pull out all entries and make the output function recursive. It's often as trivial as:

    function print_list($array, $parent=0) {
        print "
      "; foreach ($array as $row) { if ($row->parent_id == $parent) { print "
    • $row->title"; print_list($array, $row->id); # recurse print "
    • "; } } print "
    "; }

    It's only important to nest

      s into
    • . Or just use HTML and leave out the closing
    • .

      Actually this prints too many

        s, so I would check for existence of sublevels and avoid printing it directly.

提交回复
热议问题