我在PHP中进行了简单的动态导航,例如从数组中拉出HOME,ABOUT,常见问题解答,CONTACT然后显示它。现在我只需要知道如果常见问题解答在将鼠标悬停在其上之后还有4个选项可以进行编码,我还需要知道什么以及如何进行编码..我需要一些PHP编码的帮助而不是CSS。 目前我正在使用这种情况
<?php
$pages = array(
'index.php' => 'Home',
'cupping.php' => 'Cupping',
'success.php' => 'Success Stories',
'healing.php' => 'Healing',
'eating-right.php' => 'Eeating Right',
'blog.php' => 'Blog',
'faq.php' => 'FAQs',
'contact.php' => 'Contact Us',
) ;
<ul class="nav navbar-nav">
<?php foreach ($pages as $filename => $pageTitle) {
if ($filename == $currentPage) { ?>
<li class="current"><?php echo $pageTitle ; ?></li>
<?php } else { ?>
<li><a href="<?php echo $filename ; ?>"><?php echo $pageTitle ; ?></a></li>
<?php
} //if
} //foreach
?>
</ul>
现在我如何打印也有一些嵌套导航的HEALING,以便php编码后的HTML看起来像这样
<ul>
<li><ahref="index.php">Home</a></li>
<li><a href="cupping.php">Cupping</a></li>
<li><a href="success-stories.php">Success Stories</a></li>
<li><a href="healing.php">Healing Through</a></li>
<ul class="submenu">
<li><a href="herbs.php">Herbs</a></li>
<li><a href="nature.php">Nature</a></li>
<li><a href="behaviour.php">Behaviour</a></li>
</ul>
<li><a href="blog.php">Blog</a></li>
<li><a href="faqs.php">FAQs</a></li>
<li><a href="contact.php">Contact Us</a></li>
</ul>
我是php的新手,想要一些帮助。谢谢:))
答案 0 :(得分:3)
<?php
$pages = array(
'index.php' => 'Home',
'cupping.php' => 'Cupping',
'success.php' => 'Success Stories',
'healing.php' => 'Healing',
'eating-right.php' => 'Eeating Right',
'blog.php' => 'Blog',
'faq.php' => array('FAQs'=>array('submenu1.php'=>'submenu1','submenu2.php'=>'submenu2')) ,
'contact.php' => 'Contact Us',
) ;
?>
<ul class="nav navbar-nav">
<?php foreach ($pages as $filename => $pageTitle) {
if ($filename == $currentPage) {
if(is_array($pageTitle)){
foreach ($pageTitle as $menu => $value) {
echo '<li class="current">'.$menu.'</li>';
}
}else{
echo '<li class="current">'.$pageTitle.'</li>' ;
}
?>
<?php } else {
if(is_array($pageTitle)){
foreach ($pageTitle as $menu => $value) {
echo '<li><a href="#">'.$menu.'</a></li>';
echo '<ul>';
foreach ($value as $key => $submenu) {
echo '<li><a href="'.$key.'">'.$submenu.'</a></li>';
}
echo '</ul>';
}
}else{
?>
<li><a href="<?php echo $filename ; ?>"><?php echo $pageTitle ; ?></a></li>
<?php
}
} //if
} //foreach
?>
</ul>
尝试使用此代码可以帮助您为任何主菜单创建多个子菜单。
答案 1 :(得分:1)
尝试将值切换为索引。请看下面的内容。
<?php
$pages = array(
'home' => array('url' => 'index.php', 'caption' => 'Home'),
'faq' => array('url' => 'faq.php', 'caption' => 'FAQs', 'submenu' =>
array(
'submenu1' => array('url' => 'Submenu1.php', 'caption' => 'Submenu1'),
'submenu2' => array('url' => 'Submenu2.php', 'caption' => 'Submenu2')
)
),
'contact' => array('url' => 'contact.php', 'caption' => 'Contact Us'),
);
?>
<ul class="nav navbar-nav">
<?php foreach ($pages as $parent_menu) { ?>
<li class="<?= ($parent_menu['url'] == $currentPage ? 'current' : '') ?>">
<a href="<?= $parent_menu['url'] ?>"><?= $parent_menu['caption'] ?></a>
<?php if (isset($parent_menu['submenu'])) { ?>
<ul class="">
<?php foreach ($parent_menu['submenu'] as $child) { ?>
<li><a href="<?= $child['url'] ?>"><?= $child['caption'] ?></a></li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
答案 2 :(得分:1)
正如Disha V.所示, 多维 , 关联 数组就是您想要的。然后嵌套的foreach循环允许您访问不同的键。您可以在此处详细了解:https://www.safaribooksonline.com/library/view/learning-php-5/0596005601/ch04s05.html