简体   繁体   中英

Dynamic menu via PHP

The code below generates a menu with UL and LI correctly, but I needed to add other information to make the code a little different.

<?php
$items = array ( 1 => array ( 'parent' => '0', 'tit_pag' => 'Inicial', 'url_pag' => '/', 'target_url' => '_self', 'id' => '1', ), 2 => array ( 'parent' => '0', 'tit_pag' => 'Principal 1', 'url_pag' => '/', 'target_url' => '_self', 'id' => '2', 'child' => array ( 3 => array ( 'parent' => '2', 'tit_pag' => 'Item 1', 'url_pag' => 'link1.html', 'target_url' => '_self', 'id' => '3', ), 4 => array ( 'parent' => '2', 'tit_pag' => 'Item 2', 'url_pag' => 'link2.html', 'target_url' => '_self', 'id' => '4', ), 5 => array ( 'parent' => '2', 'tit_pag' => 'Item 3', 'url_pag' => 'link3.html', 'target_url' => '_self', 'id' => '5', ), ), ), 6 => array ( 'parent' => '0', 'tit_pag' => 'Principal 2', 'url_pag' => '#', 'target_url' => '_self', 'id' => '6', 'child' => array ( 7 => array ( 'parent' => '6', 'tit_pag' => 'Item 4', 'url_pag' => 'link4.html', 'target_url' => '_self', 'id' => '7', ), 8 => array ( 'parent' => '6', 'tit_pag' => 'Item 5', 'url_pag' => 'link5.html', 'target_url' => '_self', 'id' => '8', ), 9 => array ( 'parent' => '6', 'tit_pag' => 'Item 6', 'url_pag' => 'link6.html', 'target_url' => '_self', 'id' => '9', ), ), ), 10 => array ( 'parent' => '0', 'tit_pag' => 'Principal 3', 'url_pag' => 'link7.html', 'target_url' => '_self', 'id' => '10', ), 11 => array ( 'parent' => '0', 'tit_pag' => 'Principal 4', 'url_pag' => 'link8.html', 'target_url' => '_self', 'id' => '11', ), );

function get_menu($items) {
    $html = "<ul>";
    foreach($items as $key=>$value) {
        $html.= '<li><a href="/'.$value['url_pag'].'" target="'.$value['target_url'].'">'.$value['tit_pag'].'</a>';
        if(array_key_exists('child',$value)) {
            $html .= get_menu($value['child'],'child');
        }
            $html .= "</li>";
    }
    $html .= "</ul>";
    return $html;
}
print get_menu($items);
?>

Result of the above code

<ul>
    <li><a href="//" target="_self">Inicial</a></li>
    <li>
        <a href="//" target="_self">Principal 1</a>
        <ul>
            <li><a href="/link1.html" target="_self">Item 1</a></li>
            <li><a href="/link2.html" target="_self">Item 2</a></li>
            <li><a href="/link3.html" target="_self">Item 3</a></li>
        </ul>
    </li>
    <li>
        <a href="/#" target="_self">Principal 2</a>
        <ul>
            <li><a href="/link4.html" target="_self">Item 4</a></li>
            <li><a href="/link5.html" target="_self">Item 5</a></li>
            <li><a href="/link6.html" target="_self">Item 6</a></li>
        </ul>
    </li>
    <li><a href="/link7.html" target="_self">Principal 3</a></li>
    <li><a href="/link8.html" target="_self">Principal 4</a></li>
</ul>

Result I need

<ul class="nav navbar-nav">
    <li><a href="//" target="_self">Inicial</a></li>
    <li class="dropdown submenu">
        <a href="//" target="_self" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Principal 1 <i class="fa fa-angle-down" aria-hidden="true"></i></a>
        <ul class="dropdown-menu">
            <li><a href="link1.html" target="_self">Item 1</a></li>
            <li><a href="link2.html" target="_self">Item 2</a></li>
            <li><a href="link3.html" target="_self">Item 3</a></li>
        </ul>
    </li>
    <li class="dropdown submenu">
        <a href="/#" target="_self" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Principal 2 <i class="fa fa-angle-down" aria-hidden="true"></i></a>
        <ul class="dropdown-menu">
            <li><a href="/link4.html" target="_self">Item 4</a></li>
            <li><a href="/link5.html" target="_self">Item 5</a></li>
            <li><a href="/link6.html" target="_self">Item 6</a></li>
        </ul>
    </li>
    <li><a href="/link7.html" target="_self">Principal 3</a></li>
    <li><a href="/link8.html" target="_self">Principal 4</a></li>
</ul>

I was unable to modify the php code to achieve the expected result. I would be grateful if anyone could help me.

I managed to solve it by creating another function just to generate the submenus

<?php
    $items = array ( 1 => array ( 'parent' => '0', 'tit_pag' => 'Inicial', 'url_pag' => '/', 'target_url' => '_self', 'id' => '1', ), 2 => array ( 'parent' => '0', 'tit_pag' => 'Principal 1', 'url_pag' => '/', 'target_url' => '_self', 'id' => '2', 'child' => array ( 3 => array ( 'parent' => '2', 'tit_pag' => 'Item 1', 'url_pag' => 'link1.html', 'target_url' => '_self', 'id' => '3', ), 4 => array ( 'parent' => '2', 'tit_pag' => 'Item 2', 'url_pag' => 'link2.html', 'target_url' => '_self', 'id' => '4', ), 5 => array ( 'parent' => '2', 'tit_pag' => 'Item 3', 'url_pag' => 'link3.html', 'target_url' => '_self', 'id' => '5', ), ), ), 6 => array ( 'parent' => '0', 'tit_pag' => 'Principal 2', 'url_pag' => '#', 'target_url' => '_self', 'id' => '6', 'child' => array ( 7 => array ( 'parent' => '6', 'tit_pag' => 'Item 4', 'url_pag' => 'link4.html', 'target_url' => '_self', 'id' => '7', ), 8 => array ( 'parent' => '6', 'tit_pag' => 'Item 5', 'url_pag' => 'link5.html', 'target_url' => '_self', 'id' => '8', ), 9 => array ( 'parent' => '6', 'tit_pag' => 'Item 6', 'url_pag' => 'link6.html', 'target_url' => '_self', 'id' => '9', ), ), ), 10 => array ( 'parent' => '0', 'tit_pag' => 'Principal 3', 'url_pag' => 'link7.html', 'target_url' => '_self', 'id' => '10', ), 11 => array ( 'parent' => '0', 'tit_pag' => 'Principal 4', 'url_pag' => 'link8.html', 'target_url' => '_self', 'id' => '11', ), );
    
    function get_submenu($items) {
        $html2 = '<ul class="dropdown-menu">';
        foreach($items as $key=>$value) {
            $html2.= '<li><a href="/'.$value['url_pag'].'" target="'.$value['target_url'].'">'.$value['tit_pag'].'</a>';
            if(array_key_exists('child',$value)) {
                $html2 .= get_submenu($value['child'],'child');
            }
                $html2 .= "</li>";
        }
        $html2 .= '</ul>';
        return $html2;
    }               
    
    function get_menu($items) {
        $html = '<ul class="nav navbar-nav">';
        foreach($items as $key=>$value) {
            if(array_key_exists('child',$value)) {
            $html.= '<li class="dropdown submenu"><a href="/'.$value['url_pag'].'" target="'.$value['target_url'].'" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">'.$value['tit_pag'].' <i class="fa fa-angle-down" aria-hidden="true"></i></a>';
            }else{
            $html.= '<li><a href="/'.$value['url_pag'].'" target="'.$value['target_url'].'">'.$value['tit_pag'].'</a>';}
            if(array_key_exists('child',$value)) {
                $html .= get_submenu($value['child'],'child');
            }
                $html .= "</li>";
        }
        $html .= '</ul>';
        return $html;
    }
    print get_menu($items);
?>

RESULT

<ul class="nav navbar-nav">
    <li><a href="//" target="_self">Inicial</a></li>
    <li class="dropdown submenu">
        <a href="//" target="_self" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Principal 1 <i class="fa fa-angle-down" aria-hidden="true"></i></a>
        <ul class="dropdown-menu">
            <li><a href="/link1.html" target="_self">Item 1</a></li>
            <li><a href="/link2.html" target="_self">Item 2</a></li>
            <li><a href="/link3.html" target="_self">Item 3</a></li>
        </ul>
    </li>
    <li class="dropdown submenu">
        <a href="/#" target="_self" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Principal 2 <i class="fa fa-angle-down" aria-hidden="true"></i></a>
        <ul class="dropdown-menu">
            <li><a href="/link4.html" target="_self">Item 4</a></li>
            <li><a href="/link5.html" target="_self">Item 5</a></li>
            <li><a href="/link6.html" target="_self">Item 6</a></li>
        </ul>
    </li>
    <li><a href="/link7.html" target="_self">Principal 3</a></li>
    <li><a href="/link8.html" target="_self">Principal 4</a></li>
</ul>

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