导航树菜单不返回子类别

时间:2014-02-16 07:45:57

标签: php arrays

我正在尝试设置导航菜单,但我没有收回Subcats。 我认为它在。=但我不确定。

在createTree中,我调用了创建SubCat。

我想我会看一下。

提示?

若THMx端!

id  name           description  parent_id   
1   electronics    desc         0
2   cloth          kleding      0   
3   washing mach   desc was     1 
4   dryer          desc droger  1   
5   pants          desc broek   2   

    $arrayMenu = array();

    foreach( $navigation as $row ){
        $arrayMenu[$row['id']] = array("parent_id" => $row['parent_id'], "name" => $row['name']);
    }

    function createSubCat($array, $curParent){
        $html = '';
        foreach ($array as $categoryId => $category) {

            if ($curParent == $category['parent_id']) {
                $html .= '<li id="' . $categoryId . '" ><a href="#">' . $category['name'] .'</a></li>';
            }
            return $html;  
        }          
    }

    function createTree($array, $curParent) {
        $html = '';
        foreach ($array as $categoryId => $category) {
            if ($curParent == $category['parent_id']) {
                $html .= '
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">' . $category['name'] .' <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                ';
                $html .=  createSubCat($array, $categoryId);
                $html .=  '
                    </ul>
                </li>    
                ';           
            }                
            return $html;
        }
    }
    $arrayss = createTree($arrayMenu, 0);
    print_r($arrayss);

out put:

                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Electronics<b class="caret"></b></a>
                    <ul class="dropdown-menu">

                    </ul>
                </li>  

1 个答案:

答案 0 :(得分:0)

在循环内返回结果:函数在第一次迭代后结束。 它应该是:

function createSubCat($array, $curParent){
        $html = '';
        foreach ($array as $categoryId => $category) {

            if ($curParent == $category['parent_id']) {
                $html .= '<li id="' . $categoryId . '" ><a href="#">' . $category['name'] .'</a></li>';
            }
        }    
        return $html;       
    }

    function createTree($array, $curParent) {
        $html = '';
        foreach ($array as $categoryId => $category) {
            if ($curParent == $category['parent_id']) {
                $html .= '
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">' . $category['name'] .' <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                ';
                $html .=  createSubCat($array, $categoryId);
                $html .=  '
                    </ul>
                </li>    
                ';           
            }                
        }
        return $html;
    }