Build recursive nested menu from array

后端 未结 3 1742
野性不改
野性不改 2021-01-23 03:09

I\'m trying to build a recursive menu which would look something like this when finished, with an infinite amount of nested items:



        
3条回答
  •  臣服心动
    2021-01-23 03:46

    This is my example code to build nested menu for Zurb Foundation, using recursive function in PHP. Very simple and clean.

    // call the function
    build_menu();
    
    //----------------------------
    
    // recursive function
    function build_menu($parent = "", $tab=""){
        $sql = "SELECT * FROM cat WHERE parent='$parent' ORDER BY category ASC";
        $q = mysql_query($sql);
        if (mysql_num_rows($q)) {
            if (empty($parent)):
                print $tab."
      \n"; else: print $tab."\n"; } }

提交回复
热议问题