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 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>"
    ?>
    
    点赞 评论 复制链接分享

相关推荐