dsjklb0205 2010-04-17 13:45
浏览 33
已采纳

PHP动态计数和限制菜单项

I want to modify this code which works pretty good but (or I don't know because I'm new with php) I can't limit the number of li's displayed for the main elements in the menu. The actual code will echo all elements it finds, I want to limit the times

<li><a href='{$sLink}' {$sOnclick} target='_parent'>{$sPictureRep}{$sText}</a>

this line is echoed.. let's say to echo just the first 15 elements + a "MORE" button under which to display the rest of the elements as sub-menus.. (this is a 2 level horizontal menu). Can someone please help me? I really tried a lot but I'm not an expert in PHP..

Thanks!

   <?php 

require_once( '../../../inc/header.inc.php' );
require_once( DIRECTORY_PATH_INC . 'membership_levels.inc.php' );
require_once( DIRECTORY_PATH_ROOT . "templates/tmpl_{$tmpl}/scripts/TemplMenu.php" );

            class SimpleMenu extends TemplMenu
            {
            function getCode()
            {
            $this->iElementsCntInLine = 100;
            $this->getMenuInfo();
            $this->genTopItems();
            return $this->sCode;
            }

            function genTopItem($sText, $sLink, $sTarget, $sOnclick, $bActive, $iItemID, $isBold = false, $sPicture = '')
            {
            $sActiveStyle = ($bActive) ? ' id="tm_active"' : '';

            if (!$bActive) {
            $sAlt= $sOnclick ? ( ' alt="' . $sOnclick . '"' ) : '';
            $sTarget  = $sTarget  ? ( ' target="_parent"' ) : '';
            }

            $sLink = (strpos($sLink, 'http://') === false && !strlen($sOnclick)) ? $this->sSiteUrl . $sLink : $sLink;

            $sSubMenu = $this->getAllSubMenus($iItemID);

            $sImgTabStyle = $sPictureRep = '';
            if ($isBold && $sPicture != '') {
            $sPicturePath = getTemplateIcon($sPicture);
            $sPictureRep = "<img src='{$sPicturePath}' style='vertical-align:middle;width:16px;height:16px;' />";
            $sText = '&nbsp;';
            $sImgTabStyle = 'style="width:38px;"';
            }

            $sMainSubs = ($sSubMenu=='') ? '' : " {$sSubMenu} </a>";
            $this->sCode .= "

               <li><a href='{$sLink}' {$sOnclick} target='_parent'>{$sPictureRep}{$sText}</a>
                <div id='submenu'>
                <ul>
                 <li>{$sMainSubs}</li>
                </ul>
                </div>
               </li>
            ";
            }
            }

            $objMenu = new SimpleMenu();
            echo "<ul id='ddmenu'>";
            echo $objMenu->getCode();
            echo "</ul>";

    ?>
  • 写回答

1条回答 默认 最新

  • dongwei2983 2010-04-17 13:58
    关注

    It's difficult to tell exactly where you want to do this in your code, but I figure you're looking for something like:

    <?php
    for ($i = 0; i < 15; i ++){
        echo "<li><a href='{$sLink}' {$sOnclick} target='_parent'>{$sPictureRep}{$sText}</a>"
    }
    echo "<li><a href='more' target='_parent'>More...</a>"
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题