我真的想让这个工作起来并且很接近:
它循环遍历数组并创建子菜单(尽管有错误)但是: 带有子菜单项的第一个菜单正确显示,但在第二个菜单上有子菜单项,它会重复第一个子菜单项,然后是第二个子菜单项.....我错过了什么?
非常感谢任何帮助。
<ul class="nav navbar-nav">
<?php
$html = new cacheHTML('topmenu');
if(!$html->isCached){
$menuitems = getMenuLevelsArray(25, 1 , 1);
$submenuI = "0";
foreach($menuitems as $item){
if($item['submenu']){
$subs[$submenuI] = $item['submenu'];
}
?>
<li <?php if($subs[$submenuI]){ ?> class="dropdown" <?php }?>>
<a href="<?php echo $item['url']; ?>" <?php if($subs[$submenuI]){ ?> class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" <?php }?>><?php echo $item['text']; ?><?php if($subs[$submenuI]){ ?> <span class="caret"></span> <?php }?></a>
<!-- submenu begins here -->
<?php foreach($subs as $submenuI => $menu){ ?>
<ul class="dropdown-menu" role="menu">
<li>
<?php
for($a=0; $a < count($menu); $a++){
?>
<a href="<?php echo $menu[$a]['url']; ?>" <?php if(!$menu[$a+1]){ echo "class='last'"; } ?>><?php echo $menu[$a]['text']; ?></a>
<?php } ?>
</li>
</ul>
<?php $submenuI++; } ?>
</li>
<?php
}
?>
<?php } $html->show(); ?>
</ul>
`<ul class="nav navbar-nav">
<li> <a href="/">Home</a> <br>
<b>Warning</b>: Invalid argument supplied for foreach() in <b>/layout.php</b> on line 95<br>
</li>
<li class="dropdown open">
<a href="/about-us/" class="dropdown-toggle" data-toggle="dropdown" role="button">About<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="/about/">About</a>
<a href="/about/board-of-directors/">Board of Directors</a>
<a href="/about/structure/" class="last">Structure</a>
</li>
</ul>
</li>
<li>
<a href="/standards-and-codes/standards-and-codes-description/">Standards & Codes</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="/about/">About</a>
<a href="/about/board-of-directors/">Board of Directors</a>
<a href="/about/structure/" class="last">Structure</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="/resources/frequently-asked-questions/" class="dropdown-toggle" data-toggle="dropdown" role="button">Resources <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="/about/">About</a>
<a href="/about/board-of-directors/">Board of Directors</a>
<a href="/about/structure/" class="last">Structure</a>
</li>
</ul>
<ul class="dropdown-menu" role="menu">
<li>
<a href="/resources/frequently-asked-questions/">Frequently Asked Questions</a>
<a href="/news/news-archives/">News Archives</a>
<a href="/resources/resources/">Resource Links</a>
<a href="/safety-alerts/safety-alerts/" class="last">Safety Alerts</a>
</li>
</ul>
</li>
<li><a href="/contact-us/">Contact Us</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="/about/">About</a>
<a href="/about/board-of-directors/">Board of Directors</a> <a href="/about/structure/" class="last">Structure</a>
</li>
</ul>
<ul class="dropdown-menu" role="menu">
<li>
<a href="/resources/frequently-asked-questions/">Frequently Asked Questions</a>
<a href="/news/news-archives/">News Archives</a>
<a href="/resources/resources/">Resource Links</a>
<a href="/safety-alerts/safety-alerts/" class="last">Safety Alerts</a>
</li>
</ul>
</li>
</ul>`
答案 0 :(得分:0)
在第一个&#34; foreach&#34;循环你应该重置你的$ subs数组,以确保它是空的。否则它会进入第二个循环,看到旧的&#34; $ subs&#34;从上一个循环开始,再循环遍历它:
foreach ( $menuitems as $item ) {
$subs = array();
if ($item ['submenu']) {
$subs [$submenuI] = $item ['submenu'];
}
...
我发现PHP和HTML的混合难以阅读,所以我重写了代码以使我的眼睛更容易:
$html = new cacheHTML ( 'topmenu' );
if (! $html->isCached) {
$menuitems = getMenuLevelsArray ( 25, 1, 1 );
$submenuI = "0";
foreach ( $menuitems as $item ) {
if ($item ['submenu']) {
$subs [$submenuI] = $item ['submenu'];
}
echo '<li';
if($subs[$submenuI]){
echo 'class="dropdown"';
}
echo ">
<a href='{$item['url']}'";
if($subs[$submenuI]){
echo 'class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"';
}
echo ">{$item['text']}";
if($subs[$submenuI]){
echo '<span class="caret"></span>';
}
echo '</a> <!-- submenu begins here -->
';
foreach($subs as $submenuI => $menu){
echo '<ul class="dropdown-menu" role="menu">
<li>
';
for($a = 0; $a < count ( $menu ); $a ++) {
echo "<a href='{$menu[$a]['url']}'";
if(!$menu[$a+1]){
echo "class='last'";
}
echo ">{$menu[$a]['text']}</a>
";
}
echo '</li>
</ul>
';
$submenuI++;
}
echo '</li>';
}
}
$html->show(); ?>
</ul>
我希望这有帮助!
答案 1 :(得分:0)
终于搞定了...... 我需要学习更多PHP if语句和foreach的
这就是我所做的:
<ul class="nav navbar-nav">
<?php
$html = new cacheHTML('topmenu');
if(!$html->isCached){
$menuitems = getMenuLevelsArray(25, 1 , 1);
$submenuI = "0";
foreach($menuitems as $item){
if($item['submenu']){
$subs[$submenuI] = $item['submenu'];
}
?>
<li <?php if($subs[$submenuI]){ ?> class="dropdown" <?php }?>>
<a href="<?php echo $item['url']; ?>" <?php if($subs[$submenuI]){ ?> class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" <?php }?>><?php echo $item['text']; ?><?php if($subs[$submenuI]){ ?> <span class="caret"></span> <?php }?></a>
<!-- submenu begins here -->
<?php foreach($subs as $submenuI => $menu){ ?>
<ul class="dropdown-menu" role="menu">
<li>
<?php
for($a=0; $a < count($menu); $a++){
?>
<a href="<?php echo $menu[$a]['url']; ?>" <?php if(!$menu[$a+1]){ echo "class='last'"; } ?>><?php echo $menu[$a]['text']; ?></a>
<?php } ?>
</li>
</ul>
<?php $submenuI++; } ?>
</li>
<?php
}
?>
<?php } $html->show(); ?>
</ul>