Building a website using a PHP based Ecommerce product Magento.
The problem I have is that I want to use tabbed navigation.
My idea was to use CSS to show the TAB over the relevant Navigation menu item based on the URL.
However, one URL always changes, so I wanted to somehow use an ifelse statement.
I've come up with two methods that I think could work, could any experts tell me what they would think is best and how they would implement it?
Method 1
<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>
Method 2
$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>
}
If this is the final form of the menu, how about abstracting it in an array?
$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";
this way, you'd have to only edit the array in the long run.
Personally I'd do it this way:
In your head
<style>
a#<?= $currentPage ?> {
color:red;
}
</style>
Where you'll need to get the currentPage variable somehow ($_SERVER is useful).
Then just add an id of the page name to each link.
Following on from Pekkas answer, i'd change it to include a tertiary statement, sprintf and to use php template code for the foreach loop(looks neater):
$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;
Personally, I'd do it this way:
<?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>
精彩评论