简体   繁体   English

递归功能菜单树,无数据库

[英]recursive function menu tree , no DB

I have a class who add child into elements and I want to display those elements into a UL and LI with a recursive function 我有一个将子级添加到元素中的类,并且我想使用递归函数将这些元素显示到UL和LI中

I created a function, but I never do recursive before and I obviously doing it wrong :DI manage to display the children of the parent I can, and the children of if but I can go further 我创建了一个函数,但是我以前从未做过递归操作,而且我显然做错了:DI设法显示了可以的父对象的子对象,以及可以但又可以显示的父对象的子对象。

The class already created : 该类已创建:


    class MenuElement {
        public $id;
        public $children = array();

        public function __construct($id)
        {
            $this->id = $id;
        }

        public function addChild(MenuElement $menuElement)
        {
            array_push($this->children, $menuElement);
        }
    }

The creation of the elements in index.php : 在index.php中创建元素:


    $listRecursive1 = new ListRecursive(1);
    $listRecursive11 = new ListRecursive(11);
    $listRecursive12 = new ListRecursive(12);
    $listRecursive121 = new ListRecursive(121);
    $listRecursive122 = new ListRecursive(122);
    $listRecursive123 = new ListRecursive(123);
    $listRecursive1211 = new ListRecursive(1211);

    $listRecursive121->addChild($listRecursive1211);
    $listRecursive12->addChild($listRecursive121);
    $listRecursive12->addChild($listRecursive122);
    $listRecursive12->addChild($listRecursive123);
    $listRecursive1->addChild($listRecursive11);
    $listRecursive1->addChild($listRecursive12);

My function just bellow addChild : 我的功能只是在下面addChild:


    public function createList()
    {
        $html = "";

        foreach ($this->children as $child) 
        {
            $html .= "<ul><li>" . $child->id;
            //If the element have a child I create a new UL
            if ($child->children) {
                $html .= "<ul><li>" . $child->id;
                $html .= "</li></ul>";
            }
            $html .= "</li></ul>";
        }
        //Return the result
        return $html;
    }

I want to display the list like this : 我想显示这样的列表:

<ul>
    <li>1
        <ul>
            <li>11</li>
            <li>12
                <ul>
                    <li>121
                        <ul>
                            <li>1211</li>
                        </ul>
                    </li>
                    <li>122</li>
                    <li>123</li>
                </ul>
            </li>   
        </ul>
    </li>
</ul>

But this is what i got : 但这就是我得到的:

<ul>
    <li>11</li>
</ul>
<ul>
    <li>12
        <ul>
            <li>12</li>
        </ul>
    </li>
</ul>

EDIT : 编辑:

I try this : 我尝试这样:


    public function createList()
    {
        $html = "";
        foreach ($this->children as $child) {
            $html .= "<ul><li>" . $child->id;
            //If the element have a child I create a new UL
            if ($child->children) {
                    echo  "<ul>";
                    $child->createList();
                    echo "</ul>";
            }
            $html .= "</li></ul>";
        }
        //Return the result
        echo $html;
    }

And I got 我得到了

      • 1211 1211
    • 121 121
    • 122 122
    • 123 123
  • 11 11
  • 12 12

Almost there! 快好了! You just need to make a recursive call to createList() in the first version of your code, appending its result to $html . 您只需在代码的第一个版本中对createList()进行递归调用,并将其结果附加到$html

public function createList()
{
    $html = "";
    foreach ($this->children as $child) {
        $html .= "<ul><li>" . $child->id;
        if ($child->children) { 
            $html .= $child->createList();
        }
        $html .= "</li></ul>";
    }
    return $html;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM