使用基于PHP的电子商务产品Magento构建网站。
我遇到的问题是我想使用标签式导航。
我的想法是使用CSS根据URL在相关的导航菜单项上显示TAB。
但是,一个URL总是会改变,所以我想以某种方式使用ifelse语句。
我想出了两种我认为可行的方法,任何专家都可以告诉我他们认为什么是最好的以及他们将如何实施它?
<div id="nav">
<ul id="mainnav">
<li><a href="index.php" title="Welcome page" <?php if ($page == 'index.php') { ?>class="active"<?php } ?>>Welcome</a></li>
<li><a href="about_us.php" title="About us page" <?php if ($page == 'about_us.php') { ?>class="active"<?php } ?>>About us</a></li>
<li><a href="services.php" title="Services page" <?php if ($page == 'services.php') { ?>class="active"<?php } ?>>Services</a></li>
<li><a href="testimonials.php" title="Testimonials page" <?php if ($page == 'testimonials.php') { ?>class="active"<?php } ?>>Testimonials</a></li>
<li><a href="contact_us.php" title="Contact us page" <?php if ($page == 'contact_us.php') { ?>class="active"<?php } ?>>Contact us</a></li>
else
<li><a href="store.php" title="Store Page" <?php ($page == 'store.php') { ?>class="active"<?php } ?>>Store</a></li>
</ul>
</div>
$URL = store.php;
SWITCH ($sample) {
CASE home.php:
<li><a href="index.php" title="Welcome page" <?php if ($page == 'index.php') { ?>class="active"<?php } ?>>Welcome</a></li>
break;
CASE services.php:
<li><a href="services.php" title="Services page" <?php if ($page == 'services.php') { ?>class="active"<?php } ?>>Services</a></li>
break;
CASE aboutus.php:
<li><a href="about_us.php" title="About us page" <?php if ($page == 'about_us.php') { ?>class="active"<?php } ?>>About us</a></li>
break;
DEFAULT:
<li><a href="store.php" title="Store Page" <?php ($page == 'store.php') { ?>class="active"<?php } ?>>Store</a></li>
}
答案 0 :(得分:5)
如果这是菜单的最终形式,那么如何在数组中抽象呢?
$menu = array("index" => "Index",
"about_us" => "About us",
"services" => "Services",
"testimonials" => "Testimonials",
"contact_us" => "Contact us",
"store" => "Store");
/* Render the menu */
$activeFound = false;
foreach ($menu as $url => $item)
{
if ($url == $page)
{ $class = "active";
$activeFound = true;
}
else $class = "";
echo "<li><a href='$url.php' $class>".htmlentities($item)."</a></li>";
}
if (!$activeFound)
/* Render your changing store URL here */
echo "store URL";
这样,你必须长期只编辑数组。
答案 1 :(得分:2)
就我个人而言,我这样做:
在head
<style>
a#<?= $currentPage ?> {
color:red;
}
</style>
你需要以某种方式获取currentPage变量($ _SERVER很有用)。
然后只需为每个链接添加页面名称的ID。
答案 2 :(得分:1)
继Pekkas回答后,我将其更改为包含一个高级语句,sprintf并使用php模板代码进行foreach循环(看起来更整洁):
$menu = array("index" => "Index",
"about_us" => "About us",
"services" => "Services",
"testimonials" => "Testimonials",
"contact_us" => "Contact us",
"store" => "Store");
/* Render the menu */
foreach ($menu as $url => $item):
echo sprintf("<li><a href="%s.php" class="%s">%s</a></li>", $url, (($url==$page) ? "active" : ""), htmlentities($item));
endforeach;
答案 3 :(得分:0)
就个人而言,我会这样做:
<?php $act[$page] = 'active'; ?>
<div id="nav">
<ul id="mainnav">
<li><a href="index.php" title="Welcome page" class="<?= $act['index.php'] ?>">Welcome</a></li>
<li><a href="about_us.php" title="About us page" class="<?= $act['about_us.php'] ?>>About us</a></li>
<!-- and so on-->
</ul>
</div>