PHP HTML CSS初学者使用CASE/SWITCH/IF ELSE?需要建议

WAN*_*ABE 0 php magento

使用基于PHP的电子商务产品Magento构建网站.

我遇到的问题是我想使用选项卡式导航.

我的想法是使用CSS根据URL在相关的导航菜单项上显示TAB.

但是,一个URL总是会改变,所以我想以某种方式使用ifelse语句.

我想出了两种我认为可行的方法,任何专家都可以告诉我他们认为最好的方法以及他们将如何实施它?

方法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>
Run Code Online (Sandbox Code Playgroud)

方法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>
}
Run Code Online (Sandbox Code Playgroud)

Pek*_*ica 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";
Run Code Online (Sandbox Code Playgroud)

这样,您必须长期只编辑数组.