简体   繁体   中英

Menu Submenu with PHP Classes and Objects

I need to make a menu with a submenu. Menu 'id' => 'parent1' = submenu 'parent' => 'parent1'. If parent has no child echo only parent. If parent has childrens insert all children inside parent. Maybe try with a for loop, or some other solution?

If a child exists, each submenu child should be inserted in its parent by id. How?

The code example should echo:

<div class="menu" href="myurl">Parent 1
     <div class="submenu" href="myurl">Child 1</div>
     <div class="submenu" href="myurl">Child 2</div>
</div>
<div class="menu" href="myurl">Parent 2
     <div class="submenu" href="myurl">Child 3</div> 
</div>
class menu  {
    public $id;
    public $parent;
    public $name;
    public $url;
   
    public function add_node($args) {
    echo '
     <div class="menu" href="' . $args['url'] . '">' . $args['name'] . '
         
         <div class="submenu" href="' . $args['url'] . '">' . $args['name'] . '</div>
         
     </div>';
    }
}

if (class_exists('menu')){
     global $menu;
     $menu = new menu();
}   
        
function menu_parent_child(){
    $args = array(
    'id' => 'parent1', 
    'name' => 'Parent 1',
    'url' => 'myurl'
    ); 
    
    $args = array(
    'parent' => 'parent1', 
    'name' => 'Child 1',
    'url' => 'myurl'
    );
    
    $args = array(
    'parent' => 'parent1', 
    'name' => 'Child 2',
    'url' => 'myurl'
    );
    
    $args = array(
    'id' => 'parent2', 
    'name' => 'Parent 2',
    'url' => 'myurl'
    ); 
    
    $args = array(
    'parent' => 'parent2', 
    'name' => 'Child 3',
    'url' => 'myurl'
    );
}

There are a few things wrong with your code. Here are some recommendations:

  1. You are overwriting the $args array in each iteration of the menu_parent_child() function, so only the last element gets added to the menu. You should create a new array for each element.
  2. You are not adding the elements to the menu. You should call the add_node() method of the $menu object for each element.
  3. You should not have an echo statement inside the add_node() method. Instead, you should return a string that represents the HTML for the menu element.
  4. You should have a separate method for adding submenu elements to a parent element.
  5. You should keep track of the elements in the menu in a separate data structure, such as an array or an object. This will allow you to easily check if an element has children and to add children to a parent element.

Here is some sample code that demonstrates how you could implement the menu with these changes:

<?php

class Menu {
    private $elements = [];
  
    public function add_node($args) {
      $element = new stdClass();
      $element->id = $args['id'];
      $element->parent = $args['parent'] ?? null;
      $element->name = $args['name'];
      $element->url = $args['url'];
      $element->children = [];
  
      $this->elements[$element->id] = $element;
  
      if ($element->parent) {
        $this->elements[$element->parent]->children[] = $element;
      }
    }
  
    public function get_html() {
      $html = '';
      foreach ($this->elements as $element) {
        if (!$element->parent) {
          $html .= $this->get_element_html($element);
        }
      }
      return $html;
    }
  
    private function get_element_html($element) {
      $html = '<div class="menu" href="' . $element->url . '">' . $element->name;
      if (count($element->children) > 0) {
        $html .= '<div class="submenu">';
        foreach ($element->children as $child) {
          $html .= $this->get_element_html($child);
        }
        $html .= '</div>';
      }
      $html .= '</div>';
      return $html;
    }

  }
  
  if (class_exists('Menu')) {
    $menu = new Menu();
  }
  
  function menu_parent_child() {
    global $menu;
  
    $menu->add_node([
      'id' => 'parent1',
      'name' => 'Parent 1',
      'url' => 'myurl',
    ]);
  
    $menu->add_node([
      'id' => 'child1',
      'parent' => 'parent1',
      'name' => 'Child 1',
      'url' => 'myurl',
    ]);
  
    $menu->add_node([
      'id' => 'child2',
      'parent' => 'parent1',
      'name' => 'Child 2',
      'url' => 'myurl',
    ]);

    $menu->add_node([
        'id' => 'parent2',
        'name' => 'Parent 2',
        'url' => 'myurl',
      ]);

      $menu->add_node([
        'id' => 'child1',
        'parent' => 'parent2',
        'name' => 'Child 1',
        'url' => 'myurl',
      ]);
    
      $menu->add_node([
        'id' => 'child2',
        'parent' => 'parent2',
        'name' => 'Child 2',
        'url' => 'myurl',
      ]);
}

menu_parent_child();
$html = $menu->get_html();
echo $html;`enter code here`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM