使用以下PHP脚本选择我当前所在的页面。
每个页面都有变量$ page =='pagename'
此脚本工作正常但有时按钮需要翻转两次才能显示下拉菜单。
有更好的方法吗?
<div id="navigation">
<ul id="nav">
<!-- If the button HOME is selected then make the HOME Button Active -->
<?php if ($page == 'home') { ?><li><a href="index.php" a id="current">HOME</a></li>
<?php } else { ?><li><a href="index.php">HOME</a><?php } ?>
<!-- If the button ABOUT US is selected then make the ABOUT US Button Active -->
<?php if ($page == 'about-us') { ?><li><a href="about-us.php" a id="current">ABOUT US</a>
</li>
<?php } else { ?><li><a href="about-us.php">ABOUT US</a>
</li>
<?php } ?>
<!-- If the page projects is selected then make the Projects Button Active -->
<?php if ($page == 'projects') { ?><li><a href="projects.php" a id="current">PROJECTS</a>
<ul>
<li><a href="project-01.php">Project 1</a></li>
<li><a href="#">Project 2</a></li>
<li><a href="#">Project 3</a></li>
<li><a href="#">Project 4</a></li>
</ul>
</li>
<?php } else { ?><li><a href="projects.php">PROJECTS</a>
<ul>
<li><a href="project-01.php">Project 1</a></li>
<li><a href="#">Project 2</a></li>
<li><a href="#">Project 3</a></li>
<li><a href="#">Project 4</a></li>
</ul>
</li>
<?php } ?>
<!-- If the page Capabilities is selected then make the Capabilities Button Active -->
<?php if ($page == 'capabilities') { ?><li><a href="capabilities.php" a id="current">CAPABILITIES</a>
<ul>
<li><a href="capabilities-civil-works.php">Civil Works</a></li>
<li><a href="capabilities-commercial-construction.php">Commercial Construction</a></li>
<li><a href="capabilities-controlled-waste-management.php">Controlled Waste Management</a></li>
<li><a href="capabilities-plant-hire.php">Plant Hire</a></li>
</ul>
</li>
<?php } else { ?><li><a href="capabilities.php">CAPABILITIES</a>
<ul>
<li><a href="capabilities-civil-works.php">Civil Works</a></li>
<li><a href="capabilities-commercial-construction.php">Commercial Construction</a></li>
<li><a href="capabilities-controlled-waste-management.php">Controlled Waste Management</a></li>
<li><a href="capabilities-plant-hire.php">Plant Hire</a></li>
</ul>
</li>
<?php } ?>
<!-- If the page Careers is selected then make the Careers Button Active -->
<?php if ($page == 'careers') { ?><li><a href="careers.php" a id="current">CAREERS</a></li>
<?php } else { ?><li><a href="careers.php">CAREERS</a>
</li>
<?php } ?>
<!-- If the page Contact Us is selected then make the Contact Us Button Active -->
<?php if ($page == 'contactus') { ?><li><a href="contact-us.php" a id="current">CONTACT US</a></li>
<?php } else { ?><li><a href="contact-us.php">CONTACT US</a>
</li>
<?php } ?>
</ul>
<!-- Search Form -->
<div class="search-form">
<form method="get" action="#">
<input type="text" class="search-text-box" />
</form>
</div>
</div>
<div class="clear"></div>
</div>
<!-- Navigation / End -->
答案 0 :(得分:0)
第二部分可能更小:
<?php if ($page == 'projects') { ?><li><a href="projects.php" a id="current">PROJECTS</a> <?php } else { ?> <li><a href="projects.php">PROJECTS</a> <?php } ?>
<ul>
<li><a href="project-01.php">Project 1</a></li>
<li><a href="#">Project 2</a></li>
<li><a href="#">Project 3</a></li>
<li><a href="#">Project 4</a></li>
</ul>
</li>
答案 1 :(得分:0)
所以基本上你所在的页面上应该有id="current"
?这可以通过以下方式完成:
<?php
$selected = array($page => ' id="current"');
?>
...
<li><a href="index.php"<?php print $selected['home']; ?>></li>
<li><a href="about-us.php"<?php print $selected['about-us']; ?>>ABOUT US</a></li>
...
数组$selected
将有一个索引,它是当前页面,值为id="current"
。因此,此值仅应用于活动页面。
然而,这会触发undefined index notice,但您的代码中不需要任何if
。
如果你对一个更复杂的结构没问题,你可以这样做,不会得到通知。
<?php
$selected = array($page => ' id="current"');
?>
...
<li><a href="index.php"<?php print isset($selected['home'])?$selected['home']:''; ?>></li>
<li><a href="about-us.php"<?php print isset($selected['about-us'])?$selected['about-us']:''; ?>>ABOUT US</a></li>
...
由于你基本上是一遍又一遍地做同样的事情,所以我建议自动完成整个事情。
<?php
$naviPages = array(
array('name' => 'HOME', 'url' => 'index.php'),
array('name' => 'ABOUT US', 'url' => 'about-us.php'),
array('name' => 'PROJECTS' => 'url' => 'projects.php', 'children' => array(
array('name' => 'Project 1', 'url' => 'project-01.php'),
array('name' => 'Project 2', 'url' => 'project-02.php'),
)),
//...
);
//...
foreach ($naviPages as $naviPage) {
print '<li><a href="'.$naviPage['url'].'"';
if ($page == $naviPage['name'])
print ' id="active"';
print '>'.$naviPage['name'].'</a>';
if (isset($naviPage['children'])) {
print '<ul>';
foreach ($naviPage['children'] as $naviPageChild) {
print '<li><a href="'.$naviPageChild['url'].'">'.$naviPageChild['name'].'</a></li>';
}
print '</ul>';
}
print '</li>';
}
答案 2 :(得分:0)
我倾向于将$page
作为css类粘贴到您的<body>
标记或导航div中。那么你可以用纯净的CSS来做其他一切!
例如,假设你有这个:
<div id="navigation" class="page-<?php echo $page ?>">
<ul id="nav">
<li class="nav-home"><a href="index.php">HOME</a>
<li class="nav-about"><a href="about-us.php">ABOUT US</a></li>
注意导航div如何具有页面名称,并且每个菜单项都具有唯一的类名。现在你所要做的就是使用一些CSS:
.page-home .nav-home,
.page-about .nav-about {
color: red; /** assuming you want your active/current menu item to be red **/
}
看看它有多清洁?
当然,您可以将其扩展为显示/隐藏子菜单。为子菜单<ul>
标记提供唯一的类名:
<li class="nav-projects"><a href="projects.php" a id="current">PROJECTS</a>
<ul class="submenu sub-projects">
...
</ul>
还有一些CSS来控制:
.submenu {
display: none; /** Our default **/
}
.page-projects .sub-projects {
display: block; /** Show the projects sub-menu when you are on that page **/
}