[英]Need some help in PHP dynamic nested navigation
I have done simple dynamic navigation in PHP like pulling HOME, ABOUT,FAQs, CONTACT from a array a then display it. 我已经在PHP中完成了简单的动态导航,例如从数组中提取HOME,ABOUT,FAQ,CONTACT,然后显示它。 Now I just need to know what and how can I code if FAQs has 4 more options to diplay after hovering the mouse over it.. I need some help in php coding not in CSS.
现在,我只需要知道如果在将鼠标悬停在FAQ上时FAQ有4个更多选项可以进行编码,该怎么办以及如何编码。我需要一些不在CSS中的php编码帮助。 Currently I'hv this scenario
目前我有这种情况
<?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>
Now how can I print HEALING that has also some nested navigation so the HTML after php coding look like this 现在如何打印带有嵌套导航的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>
I'm new to php and want some help. 我是php新手,需要帮助。 Thanks :)
谢谢 :)
<?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>
Try this code that will help you to create multiple sub-menu for any main menu. 尝试使用此代码,它将帮助您为任何主菜单创建多个子菜单。
Try with switching value to index. 尝试将值切换为索引。 Please have a look below.
请在下面看看。
<?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>
As Disha V. has demonstrated, a multidimensional , associative array is what you want here. 由于笛莎五已经证明, 多维 , 关联数组是你想要的这里。 Then the nested foreach loop allows you to access the different keys.
然后,嵌套的foreach循环允许您访问不同的键。 You can read more about it here: https://www.safaribooksonline.com/library/view/learning-php-5/0596005601/ch04s05.html
您可以在此处了解更多信息: https : //www.safaribooksonline.com/library/view/learning-php-5/0596005601/ch04s05.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.