0

我正在尝试集成boostrap,但我正面临着我不太了解的事情...... PHP。基本上我需要的是添加 class="nav" 到 ul 和 class="dropdown-menu" 到子菜单 ul 但它重复两次导航类。这是代码。非常感谢你的帮忙!

<div class="navbar navbar-inverse">
<div class="navbar-inner">
    <div class="navbar">
        <!-- menu start -->
        <ul class="nav" id="nav">
            <li  class="level0"> <a class="dropdown-toggle" href="#"   class=""><span>Dashboard</span></a></li>
            <li onmouseover="Element.addClassName(this,'over')" onmouseout="Element.removeClassName(this,'over')" class=" active  parent dropdown level0"> <a class="dropdown-toggle" href="#"  onclick="return false" class="active"><span>Sales</span></a>
                <ul class="nav">
                    <li  class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Orders</span></a></li>
                    <li  class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Invoices</span></a></li>
                    <li  class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Shipments</span></a></li>
                    <li  class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Credit Memos</span></a></li>
                    <li  class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Transactions</span></a></li>
                    <li  class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Recurring Profiles (beta)</span></a></li>
                    <li  class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Billing Agreements</span></a></li>
                    <li  class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Terms and conditions</span></a></li>
                    <li onmouseover="Element.addClassName(this,'over')" onmouseout="Element.removeClassName(this,'over')" class="  parent dropdown last level1"> <a class="dropdown-toggle" href="#"  onclick="return false" class=""><span>Tax</span></a>
                        <ul class="nav"> <!--Need to add .dropdown-menu here-->
                            <li  class="level2"> <a class="dropdown-toggle" href="#" class=""><span>Manage Tax Rules</span></a></li>
                            <li  class="level2"> <a class="dropdown-toggle" href="#" class=""><span>Manage Tax Zones &amp; Rates</span></a></li>
                            <li  class="level2"> <a class="dropdown-toggle" href="#" class=""><span>Import / Export Tax Rates</span></a></li>
                            <li  class="level2"> <a class="dropdown-toggle" href="#" class=""><span>Customer Tax Classes</span></a></li>
                            <li  class="last level2"> <a class="dropdown-toggle" href="#" class=""><span>Product Tax Classes</span></a></li>
                        </ul>
                    </li>
                </ul>
            </li>
<!--THE PHP-->
            <?php 
            public function getMenuLevel($menu, $level = 0)
            {
                $html = '<ul class="nav" ' . (!$level ? 'id="nav"' : '') . '>' . PHP_EOL;
                foreach ($menu as $item) {
                    $html .= '<li ' . (!empty($item['children']) ? 'onmouseover="Element.addClassName(this,\'over\')" '
                        . 'onmouseout="Element.removeClassName(this,\'over\')"' : '') . ' class="'
                        . (!$level && !empty($item['active']) ? ' active' : '') . ' '
                        . (!empty($item['children']) ? ' parent dropdown' : '')
                        . (!empty($level) && !empty($item['last']) ? ' last' : '')
                        . ' level' . $level . '"> <a class="dropdown-toggle" href="' . $item['url'] . '" '
                        . (!empty($item['title']) ? 'title="' . $item['title'] . '"' : '') . ' '
                        . (!empty($item['click']) ? 'onclick="' . $item['click'] . '"' : '') . ' class="'
                        . ($level === 0 && !empty($item['active']) ? 'active' : '') . '"><span>'
                        . $this->escapeHtml($item['label']) . '</span></a>' . PHP_EOL;

                    if (!empty($item['children'])) {
                        $html .= $this->getMenuLevel($item['children'], $level + 1);
                    }
                    $html .= '</li>' . PHP_EOL;
                }
                $html .= '</ul>' . PHP_EOL;

                return $html;
            }
4

1 回答 1

1

我不能很好地测试它,因为我没有空间来放置所有这些字段和其他一些部分,但我看到你有一个递归调用的函数。这将导致第二个 'id="nav"'。

public function getMenuLevel($menu, $level = 0)
{
/* declared id="nav" here */
    $html = '<ul class="nav" ' . (!$level ? 'id="nav"' : '') . '>' . PHP_EOL;
    foreach ($menu as $item) {
        $html .= '<li ' . (!empty($item['children']) ? 'onmouseover="Element.addClassName(this,\'over\')" '
            . 'onmouseout="Element.removeClassName(this,\'over\')"' : '') . ' class="'
            . (!$level && !empty($item['active']) ? ' active' : '') . ' '
            . (!empty($item['children']) ? ' parent dropdown' : '')
            . (!empty($level) && !empty($item['last']) ? ' last' : '')
            . ' level' . $level . '"> <a class="dropdown-toggle" href="' . $item['url'] . '" '
            . (!empty($item['title']) ? 'title="' . $item['title'] . '"' : '') . ' '
            . (!empty($item['click']) ? 'onclick="' . $item['click'] . '"' : '') . ' class="'
            . ($level === 0 && !empty($item['active']) ? 'active' : '') . '"><span>'
            . $this->escapeHtml($item['label']) . '</span></a>' . PHP_EOL;

        if (!empty($item['children'])) {
/* this line calls the function again which will cause id="nav" to happen again. */
           $html .= $this->getMenuLevel($item['children'], $level + 1);
        }
        $html .= '</li>' . PHP_EOL;
    }
    $html .= '</ul>' . PHP_EOL;

    return $html;
}

修复它的一个猜测可能是更好地检查 $level 是什么,因为我认为您的 !$level 可能给出了错误的答案。

public function getMenuLevel($menu, $level = 0)
{

    $idnav = '';
    if ($level === 0) 
        $idnav = 'id="nav"';

    $html = '<ul class="nav" ' . $idnav . '>' . PHP_EOL;
    foreach ($menu as $item) {
        $html .= '<li ' . (!empty($item['children']) ? 'onmouseover="Element.addClassName(this,\'over\')" '
.....

另一个想法可能是嵌套循环。也许像下面这样:

<?php
    public function getMenuLevel($menu, $level = 0)
    {
        $html = '<ul class="nav" ' . (!$level ? 'id="nav"' : '') . '>' . PHP_EOL;
 /*notice*/
        while (!empty($item['children'])) {
            foreach ($menu as $item) {
                $html .= '<li ' . (!empty($item['children']) ? 'onmouseover="Element.addClassName(this,\'over\')" '
                    . 'onmouseout="Element.removeClassName(this,\'over\')"' : '') . ' class="'
                    . (!$level && !empty($item['active']) ? ' active' : '') . ' '
                    . (!empty($item['children']) ? ' parent dropdown' : '')
                    . (!empty($level) && !empty($item['last']) ? ' last' : '')
                    . ' level' . $level . '"> <a class="dropdown-toggle" href="' . $item['url'] . '" '
                    . (!empty($item['title']) ? 'title="' . $item['title'] . '"' : '') . ' '
                    . (!empty($item['click']) ? 'onclick="' . $item['click'] . '"' : '') . ' class="'
                    . ($level === 0 && !empty($item['active']) ? 'active' : '') . '"><span>'
                    . $this->escapeHtml($item['label']) . '</span></a>' . PHP_EOL;

              }
  /* here is the tricky part */
              $html .= '</li>' . PHP_EOL;
              $html .= $this->getMenuLevel($item['children'], $level + 1);
        }
        $html .= '</ul>' . PHP_EOL;

        return $html;
    }
?>

您可以通过反复试验来计算出棘手部分的详细信息,以使结果正确地结束列表。

于 2013-04-21T02:36:25.607 回答