SHARE
TWEET

Вложенное меню

a guest Oct 23rd, 2017 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $row[] = ['id' => '1', 'parent' => '0', 'label' => 'HOME1', 'url' => 'home1'];
  4. $row[] = ['id' => '2', 'parent' => '1', 'label' => 'HOME1-1', 'url' => 'home11'];
  5. $row[] = ['id' => '3', 'parent' => '1', 'label' => 'HOME1-1', 'url' => 'home11'];
  6. $row[] = ['id' => '4', 'parent' => '0', 'label' => 'HOME2', 'url' => 'home2'];
  7. $row[] = ['id' => '5', 'parent' => '4', 'label' => 'HOME2-2', 'url' => 'home22'];
  8. $row[] = ['id' => '6', 'parent' => '4', 'label' => 'HOME2-2', 'url' => 'home22'];
  9. $row[] = ['id' => '7', 'parent' => '0', 'label' => 'HOME3', 'url' => 'home3'];
  10. $row[] = ['id' => '8', 'parent' => '7', 'label' => 'HOME3-3', 'url' => 'home33'];
  11. $row[] = ['id' => '9', 'parent' => '7', 'label' => 'HOME3-3', 'url' => 'home33'];
  12.  
  13. foreach($row as $r){
  14.     $rows[$r['parent']][] = $r;
  15. }
  16.  
  17. function tree($rows, $parent){
  18.    
  19.     $tre = '';
  20.     if(isset($rows[$parent])){
  21.         $tre = '<ul>';
  22.         foreach($rows[$parent] as $row){
  23.            
  24.             $tre .= '<li>';
  25.             $tre .= '<a href="' . $row['url'] . '">' . $row['label'];
  26.             $tre .= tree($rows, $row['id']);
  27.             $tre .= '</a>';
  28.             $tre .= '</li>';
  29.            
  30.         }
  31.         $tre .= '</ul>';
  32.     }
  33.     return $tre;
  34. }
  35.  
  36.  
  37. echo '<pre>';
  38. echo tree($rows, 0);
  39. echo '</pre>';
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top