2014-01-21 1 views
0

부트 스트랩 navbar에서 중첩 된 카테고리를 렌더링하려하지만 원하는 출력을 얻는 데 문제가 있습니다. UL dropwdown은 필자의 반복적 인 반복 때문에 더 많은 시간을 래핑 할 수 있습니다. 내가 원하는 결과를 가지고 좋은 해결 방법을 찾을 수없는bootstrap PHP 반복적 인 반복을 사용하는 중첩 된 navbar

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; 
    } 

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',) 
     ) 

가 UPDATE

implenented 보이는 어떤 코드가 작동하지만 다음과 같이 내 $ childrenTree 배열이 보이는

가장 좋은 해결 방법은 무엇입니까?

//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

허용 대답을 살펴보면 유용 할 수 있습니다. http://stackoverflow.com/questions/21060598/create-a-ul-li-menu-from-a-tree-like-array –

+0

결과를 게시 할 수 있습니까? 'getFeedCategories()'를'var_export()'또는'var_dump()'로 사용합니까? –

답변