bootstrap嵌套导航栏,带有php递归迭代

时间:2014-01-21 09:40:09

标签: php twitter-bootstrap recursion iteration navbar

我尝试在Bootstrap导航栏中渲染我的嵌套类别,但是我有一个问题是获得所需的输出。由于我的递归迭代,UL dropwdown正在被包装更多次。我没有找到一个好的解决方法来获得所需的结果

   function getFeedCategories($db) {
        $sql = "SELECT * FROM feeds_categories";
        $datas = array();     
        $childrenTree  = [];
        $categoryNames = [];
        //We fill $childrenTree and  $categoryNames from database
       if ($db->sql($sql)) {
           while ($res = $db->getResult('array')) {
               $datas[] = $res;
           }
       } 
       foreach ($datas as $row) {           
            extract($row);
            $categoryNames[(string) $id] = $name;
            $parent = (string) $parent;
            if (!array_key_exists($parent, $childrenTree))
                $childrenTree[$parent] = array();
            $childrenTree[$parent][] = (string) $id;
        }

       return renderTreeCategories("0" ,$childrenTree,$categoryNames);
    }

    //Main recursive function. I'll asume '0' id is the root node
    function renderTreeCategories($parent, $childrenTree,$categoryNames ) {

        $children = $childrenTree[$parent];
        if (count($children) > 0) { //If node has children    
            $html .= '<li class="dropdown open">';
            $html .= '<a data-toggle="dropdown" class="dropdown-toggle" href="#">'.$categoryNames[$parent].' <b class="caret"></b></a>';
            $html .= '<ul class="dropdown-menu">';
            foreach ($children as $child){
                $html .= renderTreeCategories($child,$childrenTree,$categoryNames);
            }
            $html .= "</ul></li>";
        } else{
            $html .= '<li><a href="'.$categoryNames[$parent].'">'.$categoryNames[$parent].'</a></li>';
        }

        if ($parent != "0"){
            //$html .= "</li>";
        }

        return $html;
    }

我的$ childrenTree数组如下所示

array ( 0 =>array ( 0 => '1', 1 => '2', 2 => '5', 3 => '8', 4 => '9', 5 => '10', 6 => '11', 7 => '12', 8 => '14', ),
        1 => array ( 0 => '3', ), 
        2 => array ( 0 => '4', ), 
        5 => array ( 0 => '6', 1 => '7', ), 
       10 => array ( 0 => '13' ) 
        9 => array ( 0 => '15', 1 => '16', )
        )

更新

实现的代码似乎有效,但不确定是否是最好的解决方法。

//Main recursive function. I'll asume '0' id is the root node
function renderTreeCategories($parent, $childrenTree,$categoryNames ) {

    $children = $childrenTree[$parent];
    $has_childs = count($children);        
    if ($parent != "0") {
        if ($has_childs > 0) {
        $html .= "<li class='dropdown'>";
        $html .= '<a href="#" class="dropdown-toggle" data-toggle="dropdown">'.$categoryNames[$parent].' <b class="caret"></b></a>';
        $html .= '<ul class="dropdown-menu">';
        } else{
          $html .= "<li>";
          $html .= '<a href="">' . $categoryNames[$parent] . '</a>';
        }          
    }        
    if ($has_childs > 0) { //If node has children                
        foreach ($children as $child)
            $html .= renderTreeCategories($child, $childrenTree,$categoryNames);
            $html .= "</ul>";
    }
    if ($parent != "0") {
        $html .= "</li>";
    }
    return $html;
}

0 个答案:

没有答案