Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split menu list in two every 7 items

I got a dynamic menu that uses bootstrap, but one menu button has about 14 child items. Way too long for a website, so I want to split it in half.

This is the code that I'm trying to replicate:

<ul class="dropdown-menu dropdown-menu-large row" role="menu">
   <li class="col-sm-6">
        <ul>
            <li><a href="life-insurance.html">Life Insurance</a></li>
            <li><a href="life-insurance.html">Home Insurance</a></li>
            <li><a href="life-insurance.html">Travel Insurance</a></li>
            <li><a href="life-insurance.html">Pet Insurance</a></li>
        </ul>
   </li>

   <li class="col-sm-6">
        <ul>
            <li><a href="life-insurance.html">Boat Insurance</a></li>
            <li><a href="life-insurance.html">Auto Insurance</a></li>
            <li><a href="life-insurance.html">Bike Insurance</a></li>
            <li><a href="life-insurance.html">Business Insurance</a></li>
        </ul>
   </li>
</ul>

This is what I tried:

$tel = 1;

foreach ( $nmenuccr as $cmenu ) {

    // If id matches and category id is 11 (services) split in half
    if ( $cmenu['id'] && $cmenu['catid'] == '11' ){

        if($tel == 1) {
            $hmenu .= '<li class="col-sm-6"><ul>';  
        }


        $hmenu.= '
                <li><a href="'.$cmenu['alias'].'.html">'.$cmenu['title'].'</a></li>
            ';

        if(($tel % 7) == 0){
            $hmenu .= '</ul></li> <li class="col-sm-6"><ul>'; 
        }
        $tel++;     


        if(($tel % 7) != 0){
            $menu .= '</li>';
        }
    //Else use the normal dropdown layout
    }else{

        if (strlen($cmenu['title']) > 25){  
           $shortstrmen = substr($cmenu['title'], 0, 25) . '...';

           $hmenu.= '
                <li><a href="'.$cmenu['alias'].'.html">'.$shortstrmen.'</a>
            ';
        }else{
        $hmenu.= '
                <li><a href="'.$cmenu['alias'].'.html">'.$cmenu['title'].'</a>
            ';
        }

    }

}

However this returns the following code:

https://jsfiddle.net/sms16v44/

Does anyone see what I am doing wrong?

like image 609
twan Avatar asked Mar 26 '26 13:03

twan


1 Answers

Instead of trying to alter your code, I came up with the following. This allows you to automatically create sub menu's out of a menu. I hope it helps out.

It creates it like the following screenshot:

enter image description here

and the other function/way:

enter image description here

<?php

// recreating dummy menu data
function createDummyList() {
    $nmenuccr = array();
    for($i=0;$i<24;$i++) {
        $a = array();
        $a['id']=$i;
        $a['catid']=$i;
        $a['alias']="alias $i";
        $a['title']="title $i";
        array_push($nmenuccr, $a);
    }
    return $nmenuccr;
}


/**
 * Parse a menu into smaller menu's
 * @param type $menu the main menu
 * @param type $class html class
 * @param type $max_sub_items max items before creating a new submenu
 * @param type $submenu (used for recursion)
 * @return string
 */
function createMenuList(&$menu, $class, $max_sub_items) {    

    $out = "<ul class='$class'><li>";

    // shift items
    $submenu = array();
    $i=0;
    $loop=0;
    while(count($menu) > 0) {        

        array_push($submenu, array_shift($menu));

        $i++;
        if ($i == $max_sub_items || count($menu) == 0) {
            $out .= createList($submenu, $loop);
            $out .= '<li>In between</li>';
            $i=0;
            $submenu = array();
            $loop++;            
        }
    }           

    $out .= "</li></ul>";

    return $out;
}

/**
 * 
 * @param type $list
 * @param type $submenu_label
 * @return string
 */
function createList(&$list, $loop) {

    $out = '<ul>';
    foreach($list as $l) {
        $out .= '<li><a href="'.$l['alias'].'.html">'.$l['title'].'</a></li>';    
    }
    $out .= '</ul>';
    return $out;
}


/**
 * Parse a menu into smaller menu's
 * @param type $menu the main menu
 * @param type $class html class
 * @param type $max_sub_items max items before creating a new submenu
 * @param type $submenu (used for recursion)
 * @return string
 */
function createSubMenuList(&$menu, $class, $max_sub_items, $submenu = 0) {

    // create new list
    $out = "<ul class='$class'>";

    // shift items
    for($i=0;$i<$max_sub_items;$i++) {              

       $item = array_shift($menu);

        // add item to list
        if (isset($item)) {
            $out .= '<li><a href="'.$item['alias'].'.html">'.$item['title'].'</a></li>';   
        }
    }

    // check if we're done
    if (count($menu) > 0) {           
        $submenu++;
        $out .= "<li class='submenu_$submenu'>";
        // create submenu in parent menu
        $out .= createSubMenuList($menu, $class, $max_sub_items, $submenu);
        $out .= "</li>";
    }

    $out .= "</ul>";

    return $out;

}

// call menu creation function
$list1=createDummyList();
$list2=createDummyList();
echo createSubMenuList($list1, 'hoofdmenu', 7);
echo "<hr />";
echo createMenuList($list2, 'hoofdmenu', 7);

?>
like image 131
vaultboy Avatar answered Mar 28 '26 02:03

vaultboy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!