2013-04-11 118 views
0

我想要genreate父親 - 孩子和他們的孩子等 我有一個數組,其子項意味着子項目。在PHP中的父母孩子和他們的孩子的關係

$navarray= Array 
(
    [0] => Array 
     (
      [id] => 3 
      [name] => add 
      [controller] => ClassYearsSubjectsController 
      [action] => getsubjects 
      [parent] => 0 
      [sub] => Array 
       (
       ) 

     ) 

    [1] => Array 
     (
      [id] => 1 
      [name] => occupations 
      [controller] => OccupationsController 
      [action] => index 
      [parent] => 0 
      [sub] => Array 
       (
        [0] => Array 
         (
          [id] => 7 
          [name] => test 
          [controller] => AddressesController 
          [action] => index 
          [parent] => 1 
          [sub] => Array 
           (
            [0] => Array 
             (
              [id] => 8 
              [name] => testnew 
              [controller] => AdmissionEnquiriesController 
              [action] => giveadmissions 
              [parent] => 7 
              [sub] => Array 
               (
               ) 

             ) 

           ) 

         ) 

        [1] => Array 
         (
          [id] => 2 
          [name] => add 
          [controller] => OccupationsController 
          [action] => add 
          [parent] => 1 
          [sub] => Array 
           (
           ) 

         ) 

       ) 

     ) 

) 

我想ot生成樹。 但我想

<?php echo GenerateNavHTML($navarray); ?> 

和功能GenerateNavHTML

<?php 

function GenerateNavHTML($nav) 
{ 

    $html = ''; 
    foreach($nav as $page) 
    { 
     $html.='<ul><li>';  

     $html .= '<a href="#">k</a>'; 
     $html .= GenerateNavHTML($page['sub']); 
     $html .= '</li></ul>'; 
    } 
    return $html; 
} 

    ?> 

而且我的輸出是

<ul style="display: block;" class="nav"> 
    <li class="test"><a href="">add</a> 
    </li> 
</ul><ul style="display: block;" class="nav"><li class="test"><a href="">occupations</a><ul style="display: block;" class="nav"><li class="test"><a href="">test</a><ul style="display: block;" class="nav"><li class="test"><a href="">testnew</a></li></ul></li></ul><ul style="display: block;" class="nav"><li class="test"><a href="">add</a></li></ul></li></ul> 

,但我期待着這個輸出。

<ul style="display: block;" class="nav"> 
    <li class="test"> 
    <li class="test"> 
     <a class="parent" href="#">Shoes</a> 
     <ul> 
      <li> 
       <a class="parent" href="#">Womens</a> 
       <ul> 
        <li><a href="#">Sandals</a></li> 
        <li><a href="#">Sneakers</a></li> 
        <li><a href="#">Wedges</a></li> 
        <li><a href="#">Heels</a></li> 
        <li><a href="#">Loafers</a></li> 
        <li><a href="#" class="last" >Flats</a></li> 
       </ul> 
      </li> 
      <li> 
       <a class="parent last" href="#">Mens</a> 
       <ul> 
        <li><a href="#">Loafers</a></li> 
        <li><a href="#">Sneakers</a></li> 
        <li><a href="#">Formal</a></li> 
       </ul> 
      </li> 
     </ul> 
    </li> 
    </ul> 
Can anybody tell what logic i need to change 
Thanks 
+0

看到的數據存儲位置的wordpress層次類別結構 – 2013-04-11 12:25:24

+0

?在Mysql中? – ripa 2013-04-11 12:31:19

+0

我有上面提到的數組 – Rashmi 2013-04-11 12:32:17

回答

1

試試這個

<?php 

function GenerateNavHTML($nav) 
{ 
    if(!empty($nav)) { 

      if(isset($nav[0]['parent'])) { 
       $parentid=$nav[0]['parent']; 
      }else{ 
       $parentid=1; 
      } 
     if($parentid==0) { 
      $html = '<ul style="display: block;" class="nav">'; 
     }else { 
      $html='<ul>'; 
     } 
      foreach($nav as $page) { 
       $html.='<li>';  
       $html .= '<a href="#">k</a>'; 
       $html .= GenerateNavHTML($page['sub']); 
       $html .= '</li>'; 
      } 
      $html.='</ul>'; 
      return $html; 
    } 
} 

    ?> 
0

嘗試這樣做:

<?php 

function GenerateNavHTML($nav) 
{ 
    $html = '<ul>'; 
    foreach ($nav as $page) { 
     $html.='<li>'; 
     $html .= '<a href="#">k</a>'; 
     $html .= GenerateNavHTML($page['sub']); 
     $html .= '</li>'; 
    } 

    $html .= '</ul>'; 
    return $html; 
} 
0

你可以試試這個方法:

function GenerateNavHTML($nav) 
{ 
    if (empty ($nav)) 
     return; 

    $html = ''; 
    $html.='<ul>'; 

     foreach($nav as $page) 
     { 
      $html.='<li>'; 
       $html .= '<a href="#">'.$page["name"].'</a>'; 
       $html .= GenerateNavHTML($page['sub']); 
      $html .= '</li>'; 
     } 

    $html.='</ul>'; 

    return $html; 
} 

獲取UL標記出的每個循環。