无法获得递归功能

时间:2014-03-19 15:12:13

标签: php function recursion menu nav

我正在尝试构建一个导航菜单,该菜单从数据库填充导航的前几个级别,然后循环以重新运行部分代码以查找任何其他子级别。

下面的代码完美地用于填充前几个级别,但如果我尝试将----------行之间的所有内容放入一个函数然后在其自身内调用该函数,则会中断。我无法弄清楚我做错了什么,并且真的很感激一些新鲜的眼睛去看看并告诉我,如果我犯了一个愚蠢的错误或者我做的方式它无法运作。如果它无法按照我的方式工作,那么我可以做一些调整来使它工作吗?

感谢。

P.S。第一个代码块=当前工作代码加上---------行,第二个代码块是所有代码,包括我尝试构建和调用loopMenu函数的方式。第三个块是我如何调用buildMenu6函数。

所以这可以按预期工作:

<?php
function buildMenu6 ($allpages, $childpageids, $childpages, $subchildpages) {
    foreach ($childpageids as $childid){
        $c[] = $childid['id'];
    };
    echo "<ul>\n";  
    foreach ($allpages as $pages){
        $a = $pages['parentid'];
        $b = $pages['id'];
        if (!in_array($b, $c)){
            echo "<li><a href=" . $pages['url'] . ">" . $pages['linklabel'] . "</a>";
            //------------------------------------------------------------------    
            echo"<ul>\n";
            foreach ($childpages as $childparent) {
                $d = $childparent['parentid'];
                $e = $childparent['id'];
                if (($d == $b) or ($d == $g)) {
                    echo "<li><a href=" . $childparent['url'] . ">" . $childparent['linklabel'] . "</a>";
                    echo "<ul>\n";
                    foreach ($subchildpages as $subchild){
                        $g = $subchild['id'];
                        $f = $subchild['parentid'];
                        if ($f == $e){
                            echo "<li><a href=" . $subchild['url'] . ">" . $subchild['linklabel'] . "</a>";
                            echo "<li/>";
                        };
                    };
                    echo"</ul>\n";
                    echo "</li>";
                };
            };
            echo "</ul>\n";
            //---------------------------------------------------------         
            echo "</li>";
        };
    };
    echo "</ul>\n";
}; ?>

这并没有按预期工作:(ps // START LOOPMENU FUNCTION和其他//不是原始代码,我只是为了便于识别而加入它们)

<?php
function buildMenu6 ($allpages, $childpageids, $childpages, $subchildpages) {
    foreach ($childpageids as $childid){
        $c[] = $childid['id'];
    };

    echo "<ul>\n";  
    foreach ($allpages as $pages){
        $a = $pages['parentid'];
        $b = $pages['id'];

        if (!in_array($b, $c)){
            echo "<li><a href=" . $pages['url'] . ">" . $pages['linklabel'] . "</a>";
            function loopMenu($childpages, $subchildpages){ //START LOOPMENU FUNCTION
                echo"<ul>\n";
                foreach ($childpages as $childparent) {
                    $d = $childparent['parentid'];
                    $e = $childparent['id'];
                    if (($d == $b) or ($d == $g)) {
                        echo "<li><a href=" . $childparent['url'] . ">" . $childparent['linklabel'] . "</a>";
                        echo "<ul>\n";
                        foreach ($subchildpages as $subchild){
                            $g = $subchild['id'];
                            $f = $subchild['parentid'];
                            if ($f == $e){
                                echo "<li><a href=" . $subchild['url'] . ">" . $subchild['linklabel'] . "</a>";
                                loopMenu($childparent, $subchild); // CALL FUNCTION LOOPMENU
                                echo "<li/>";
                            };
                        };

                        echo"</ul>\n";
                        echo "</li>";

                    };
                };
                echo "</ul>\n";

            };          //END LOOPMENU FUNCTION 

            loopMenu($childpages, $subchildpages); //CALL FUNCTION LOOPMENU

            echo "</li>";
        };

    };
    echo "</ul>\n";
}; ?>

最后我像这样调用buildMenu6:

<?php buildMenu6($pageids, $childPageIds, $childPageArray, $childPageArray); ?>

任何帮助都会非常感激,因为我已经连续几天与他们合作了!

0 个答案:

没有答案