繁体   English   中英

PHP HTML CSS初学者使用案例/开关/如果还可以? 需要建议

[英]PHP HTML CSS Beginner USE CASE / SWITCH / IF ELSE? Need advice

使用基于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>

方法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>
}

如果这是菜单的最终形式,那么如何将其抽象为数组?

$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";

这样,从长远来看,您只需要编辑阵列。

我个人会这样:

在你的head

<style>
a#<?= $currentPage ?> {
    color:red;
}
</style>

您需要以某种方式获取currentPage变量的地方($ _SERVER很有用)。

然后,只需将页面名称的ID添加到每个链接即可。

在Pekkas回答之后,我将其更改为包括第三条语句sprintf,并为foreach循环使用php模板代码(看起来更整洁):

$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;

就个人而言,我会这样:

<?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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM