douershuang7356 2016-04-12 08:23
浏览 81
已采纳

每7个项目分为两个菜单列表

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?

  • 写回答

5条回答 默认 最新

  • doula4096 2016-04-21 07:32
    关注

    Keepin it a junky piece of code, you can just quick-fix it this way:

    $tel = 0;
    
    /* .. */
    
    // 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){
            if ($tel == 7) {
                $hmenu .= '</ul></li><li class="col-sm-6"><ul>'; 
            } else {
                $hmenu .= '</ul></li>';
            }
        }
    //Else use the normal dropdown layout
    } // ..
    

    Working example (updated): http://sandbox.onlinephpfunctions.com/code/13fd6974cd66c847747f44a4be9b892aa47e4979

    But you should refactor your $nmenuccr through a function, which will make it an array reflecting the target structure of menu to KISS the view-generating part of code.

    Edit: updated operations on $tel.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?