Sort Array with PHP to generate a list of multilevel menu

0

I'm trying to generate a multilevel menu listing in PHP but I have not managed to do it, I would like to help me with simple examples and sort the Categories and Sub categories with their title, something like this:

  • Category 1
  •     
  •         TITLE HERE         
    • Category Child 1
    •             
    • Category Child 2
    •             
    • Category Child 3
    •             
    •                 TITLE HERE                 
      • Category child 1
      •                     
      • Category child 2
      •                     
      • Category child 3
      •                 
    •         
  •     
  • Category 1
  •     
  • Category 2
  •     
  • Category 3

I have an array in PHP:

$data = Array (
    '0' => Array (
        'id' => '1',
        'id_parent' => '',
        'name' => 'Sports'
    ),

    '1' => Array (
        'id' => '2',
        'id_parent' => '1',
        'name' => 'Football'
    ),

    '2' => Array (
        'id' => '3',
        'id_parent' => '1',
        'name' => 'Bascket'
    ),

    '3' => Array (
        'id' => '4',
        'id_parent' => '',
        'name' => 'Health'
    ),

    '4' => Array (
        'id' => '5',
        'id_parent' => '4',
        'name' => 'Nutrition and diet'
    ),

    '5' => Array (
        'id' => '6',
        'id_parent' => '4',
        'name' => 'Beauty Salon'
    ),

    '6' => Array (
        'id' => '7',
        'id_parent' => '',
        'name' => 'Films'
    ),

    '7' => Array (
        'id' => '8',
        'id_parent' => '7',
        'name' => 'Armageddon'
    ),

    '8' => Array (
        'id' => '9',
        'id_parent' => '7',
        'name' => 'Apocalypse'
    ),

    '9' => Array (
        'id' => '10',
        'id_parent' => '',
        'name' => 'News'
    ),

    '10' => Array (
        'id' => '11',
        'id_parent' => '10',
        'name' => 'International'
    ),

    '11' => Array (
        'id' => '12',
        'id_parent' => '11',
        'name' => 'News from Syria'
    ),

    '12' => Array (
        'id' => '13',
        'id_parent' => '11',
        'name' => 'News from Palestine'
    )
);

I have a table in MySQL:

CREATE TABLE 'categories' (
  'id' mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  'id_parent' mediumint(8) unsigned DEFAULT NULL,
  'name' varchar(100) NOT NULL,
  PRIMARY KEY ('id'),
  UNIQUE KEY 'FK_categories_UNIQUE' ('name'),
  KEY 'FK_categories' ('id_parent'),
  CONSTRAINT 'FK_categories' FOREIGN KEY ('id_parent') REFERENCES 'categories' ('id') ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of categories
-- ----------------------------
INSERT INTO 'categories' VALUES ('1', null, 'Sports');
INSERT INTO 'categories' VALUES ('2', '1', 'Football');
INSERT INTO 'categories' VALUES ('3', '1', 'Bascket');
INSERT INTO 'categories' VALUES ('4', null, 'Health');
INSERT INTO 'categories' VALUES ('5', '4', 'Nutrition and diet');
INSERT INTO 'categories' VALUES ('6', '4', 'Beauty Salon');
INSERT INTO 'categories' VALUES ('7', null, 'Films');
INSERT INTO 'categories' VALUES ('8', '7', 'Armageddon');
INSERT INTO 'categories' VALUES ('9', '7', 'Apocalypse');
INSERT INTO 'categories' VALUES ('10', null, 'News');
INSERT INTO 'categories' VALUES ('11', '10', 'International');
INSERT INTO 'categories' VALUES ('12', '11', 'News from Syria');
INSERT INTO 'categories' VALUES ('13', '11', 'News from Palestine');

I appreciate your help very much, please.

    
asked by Learning and sharing 12.07.2017 в 02:44
source

1 answer

1

You put the menu in two formats, in an array format and in a Database format. I do not know which one you are going to use. I understand that the array is taken from the database and the only thing that is missing is converting the array in the menu.

Recursive programming is used in this code. It is a function that calls itself repeatedly. In this case to remove each of the menus and submenus.

function devolverNivelRecursivo( $padre, $data ) {

   $arr = array();    

   // Sacando elementos hijos del padre indicado
   foreach( $data as $elem ) {
       if ( $elem['id_parent'] == $padre) {
           $arr[] = $elem;
       }
   }

   if ( count($arr) > 0 ) {
       echo "\n<ul>\n";
       foreach( $arr as $menu ) {
          echo "<li><span>" . $menu['name'] . "</span>";

          // Busco si el elemento tiene hijos
          devolverNivelRecursivo($menu['id'], $data);

          echo "</li>\n";
       }
       echo "</ul>\n";
   }
}

// Funcion para ordenar el array
function comp($x, $y) {
    if ($x['name'] == $y['name']) {
        return 0;
    }
    return ($x['name'] < $y['name']) ? -1 : 1;
}

// Ordenamos el menu (sin tener en cuenta niveles) por el campo name
uasort($data, 'comp');

// llamo a la funcion recursiva indicandole padre vacio (raíz)
devolverNivelRecursivo( '', $data );
?>
    
answered by 12.07.2017 / 09:10
source