Generate an html menu with an array in php

1

I have this array:

 <?php
    $MENU = array();

    $MENU["ESCRITORIO"] = array
        (
        'emparentar' => true, // emparentar == true ? muestra en el nivel superior menu : no lo muestra en el nivel superior
        'abilitado' => true,
        'text' => 'Escritorio',
        'link' => 'public/escritorio',
        'sub_modules' => array()   // si esta vacia es que no tiene sub menus
    );

    $MENU["ODONTOLOGIA"] = array
        (
        'emparentar' => true,
        'abilitado' => true,
        'text' => 'Odontologia',
        'link' => 'public/shop',
        'sub_modules' => array('PACIENTES', 'CONSULTAS', 'RECIPES', 'CITAS') // PACIENTES, CONSULTAS , RECIPES, CITAS son un sub menu de odontologia y estan configurados mas abajo
    );
    $MENU['PACIENTES'] = array(
        'emparentar' => false,
        'abilitado' => true,
        'text' => 'Pacientes',
        'link' => 'public/odontologia/pacientes',
        'sub_modules' => array()
    );

    $MENU['CONSULTAS'] = array(
        'emparentar' => false,
        'abilitado' => true,
        'text' => 'Consultas',
        'link' => 'public/odontologia/consultas',
        'sub_modules' => array()
    );

    $MENU['RECIPES'] = array(
        'emparentar' => false,
        'abilitado' => true,
        'text' => 'Recipes',
        'link' => 'public/odontologia/recipes',
        'sub_modules' => array('INDICACIONES')
    );
    $MENU['INDICACIONES'] = array(
        'emparentar' => false,
        'abilitado' => true,
        'text' => 'Indicaciones',
        'link' => 'public/odontologia/recipes/indicaciones',
        'sub_modules' => array()
    );
    $MENU['CITAS'] = array(
        'emparentar' => false,
        'abilitado' => true,
        'text' => 'Citas',
        'link' => 'public/odontologia/citas',
        'sub_modules' => array()
    );
    $MENU['ORTODONCIA'] = array
        (
        'emparentar' => TRUE,
        'abilitado' => TRUE,
        'text' => 'Ortodoncia',
        'link' => 'public/ortodoncia',
        'sub_modules' => array('ORTPACIENTES', 'ORTCONSULTA', 'ORTRECIPE', 'ORTCITAS')
    );

    $MENU['ORTPACIENTES'] = array(
        'emparentar' => FALSE,
        'abilitado' => TRUE,
        'text' => 'Pacientes',
        'link' => 'public/ortodoncia/pacientes',
        'sub_modules' => array()
    );

    $MENU['ORTCONSULTA'] = array(
        'emparentar' => FALSE,
        'abilitado' => TRUE,
        'text' => 'Consultas',
        'link' => 'public/ortodoncia/consultas',
        'sub_modules' => array()
    );

    $MENU['ORTRECIPE'] = array(
        'emparentar' => FALSE,
        'abilitado' => TRUE,
        'text' => 'Recipes',
        'link' => 'public/ortodoncia/recipes',
        'sub_modules' => array('ORTINDICACIONES')
    );
    $MENU['ORTINDICACIONES'] = array(
        'emparentar' => FALSE,
        'abilitado' => TRUE,
        'text' => 'Indicaciones',
        'link' => 'public/ortodoncia/indicaciones',
        'sub_modules' => array()
    );

    $MENU['ORTCITAS'] = array(
        'emparentar' => FALSE,
        'abilitado' => TRUE,
        'text' => 'Citas',
        'link' => 'public/ortodoncia/citas',
        'sub_modules' => array()
    );
    $MENU['CONFIGURACION'] = array(
        'emparentar' => TRUE,
        'abilitado' => TRUE,
        'text' => 'Configuracion',
        'link' => 'public/configuracion',
        'sub_modules' => array()
    );
    $MENU['REPORTES'] = array(
        'emparentar' => TRUE,
        'abilitado' => TRUE,
        'text' => 'Reportes',
        'link' => 'public/Reportes',
        'sub_modules' => array()
    );
    $MENU['SALIR'] = array(
        'emparentar' => TRUE,
        'abilitado' => true,
        'text' => 'Salir',
        'link' => 'public/salir',
        'sub_modules' => array()
    );

also to go through that array I have this code

 function show_menu($MENU, $subIndex = false) {

        $menu_string = '<ul>';


        if (!$subIndex) {
            foreach ($MENU as $item) {
                if ($item['abilitado'] && $item['emparentar']) {
                    $_subString = "";
                    if (!empty($item['sub_modules'])) {
                        foreach ($item['sub_modules'] as $sub) {
                            $_subString .= show_menu($MENU, $sub);
                        }
                    }
                    $menu_string .= '<li>' . '<a href="' . $item['link'] . '">' . $item['text'] . '</a>' . $_subString . '</li>';
                }
            }
        } else {
            if ($MENU[$subIndex]['abilitado'] && !$MENU[$subIndex]['emparentar']) {
                $_subString = "";
                if (!empty($MENU[$subIndex]['sub_modules'])) {
                    foreach ($MENU[$subIndex]['sub_modules'] as $sub) {
                        $_subString .= show_menu($MENU, $sub);
                    }
                }
                $menu_string .= '<li>' . '<a href="' . $MENU[$subIndex]['link'] . '">' . $MENU[$subIndex]['text'] . '</a>' . $_subString . '</li>';
            }
        }

        return $menu_string . '</ul>';
    }

The error I have is that when generating the menu in the view is generated well but when I go to inspect the item in the browser appears as follows:

 <ul>
        <li>
            <a href="public/escritorio">Escritorio</a>
        </li>
        <li>
            <a href="public/shop">Odontologia</a>
            <!-- Aqui comienza el error -->
            <ul>
                <li>
                    <a href="public/odontologia/pacientes">Pacientes</a>
                </li>
            </ul>
            <ul>
                <li>
                    <a href="public/odontologia/consultas">Consultas</a>
                </li>
            </ul>
            <ul>
                <li>
                    <a href="public/odontologia/recipes">Recipes</a>
                    <ul>
                        <li>
                            <a href="public/odontologia/recipes/indicaciones">Indicaciones</a>
                        </li>
                    </ul>
                </li>
            </ul>
            <ul>
                <li>
                    <a href="public/odontologia/citas">Citas</a>
                </li>
            </ul>
        </li>
        <li>
            <a href="public/ortodoncia">Ortodoncia</a>
            <!--Aqui se repite el error-->
            <ul>
                <li>
                    <a href="public/ortodoncia/pacientes">Pacientes</a>
                </li>
            </ul>
            <ul>
                <li>
                    <a href="public/ortodoncia/consultas">Consultas</a>
                </li>
            </ul>
            <ul>
                <li>
                    <a href="public/ortodoncia/recipes">Recipes</a>
                    <ul>
                        <li>
                            <a href="public/ortodoncia/indicaciones">Indicaciones</a>
                        </li>
                    </ul>
                </li>
            </ul>
            <ul>
                <li>
                    <a href="public/ortodoncia/citas">Citas</a>
                </li>
            </ul>
        </li>
        <li>
            <a href="public/configuracion">Configuracion</a>
        </li>
        <li>
            <a href="public/Reportes">Reportes</a>
        </li>
        <li>
            <a href="public/salir">Salir</a>
        </li>
    </ul>

as you could see at the time of generating a sub menu the tag < ul > it is repeated for each item of the submenu and should be a label one for each item group of a submenu.

    
asked by jsoe jose 26.06.2016 в 13:48
source

2 answers

1

The problem is that every time you call the function show_menu() the <ul></ul> is included without any condition, I will propose a quick solution, based on the existing code, although there are better ways to do it (which I might need to rewrite the function a little bit.)

I also assume that when the function is called to generate the menu, the value of the input parameter $subIndex is false.

function show_menu($MENU, $subIndex = false) {

    if (!$subIndex) {

        $menu_string = '';
        // agregamos <ul> solo en el primer nivel
        $menu_string = '<ul>';

        foreach ($MENU as $item) {
            if ($item['abilitado'] && $item['emparentar']) { // debería ser 'habilitado'
                $_subString = "";
                if (!empty($item['sub_modules'])) {

                    // agregamos <ul></ul> alrededor del iterador para los elementos que tienen $subIndex
                    $_subString .= '<ul>';
                    foreach ($item['sub_modules'] as $sub) {
                        $_subString .= show_menu($MENU, $sub);
                    }
                    $_subString .= '</ul>';

                }
                $menu_string .= '<li>' . '<a href="' . $item['link'] . '">' . $item['text'] . '</a>' . $_subString . '</li>';
            }
        }

        // de nuevo cerramos solo en el primer nivel
        $menu_string = $menu_string . '</ul>';

    } else {
        if ($MENU[$subIndex]['abilitado'] && !$MENU[$subIndex]['emparentar']) {
            $_subString = "";
            if (!empty($MENU[$subIndex]['sub_modules'])) {
                foreach ($MENU[$subIndex]['sub_modules'] as $sub) {
                    $_subString .= show_menu($MENU, $sub);
                }
            }
            $menu_string .= '<li>' . '<a href="' . $MENU[$subIndex]['link'] . '">' . $MENU[$subIndex]['text'] . '</a>' . $_subString . '</li>';
        }
    }

    return $menu_string;
}
    
answered by 26.06.2016 / 15:19
source
0

Here is the 100% functional function:

 function show_menu($MENU, $subIndex = false) {
        $menu_string = '';
        if (!$subIndex) {


            // agregamos <ul> solo en el primer nivel
            $menu_string = '<ul>';

            foreach ($MENU as $item) {
                if ($item['habilitado'] && $item['emparentar']) { // debería ser 'hhabilitado'
                    $_subString = "";
                    if (!empty($item['sub_modules'])) {

                        // agregamos <ul></ul> alrededor del iterador para los elementos que tienen $subIndex
                        $_subString .= '<ul>';
                        foreach ($item['sub_modules'] as $sub) {
                            $_subString .= show_menu($MENU, $sub);
                        }
                        $_subString .= '</ul>';
                    }
                    $menu_string .= '<li>' . '<a href="' . $item['link'] . '">' . $item['text'] . '</a>' . $_subString . '</li>';
                }
            }

            // de nuevo cerramos solo en el primer nivel
            $menu_string = $menu_string . '</ul>';
        } else {
            if ($MENU[$subIndex]['habilitado'] && !$MENU[$subIndex]['emparentar']) {
                $_subString = "";

                if (!empty($MENU[$subIndex]['sub_modules'])) {
                    //agregamos <ul> para los submenus de los submenus jajaja
                    $_subString.='<ul>';
                    foreach ($MENU[$subIndex]['sub_modules'] as $sub) {
                        $_subString .= show_menu($MENU, $sub);
                    }
                    $_subString.='</ul>';
                }
                $menu_string .= '<li>' . '<a href="' . $MENU[$subIndex]['link'] . '">' . $MENU[$subIndex]['text'] . '</a>' . $_subString . '</li>';
            }
        }

        return $menu_string;
    }

I have already corrected the error that I mentioned that it could only be 2 levels.

    
answered by 26.06.2016 в 16:29