创建一个 PHP 菜单,可以突出显示当前选项卡。

3
所以我有一个菜单在一个php文件中,看起来像这样(这是整个文件。我完全是新手PHP)。
menu.php:
<li id="current"><a href="#"><span>Home</span></a></li> 
<li><a href="http://blog.me.net/"><span>Blog</span></a></li> 
<li><a href="http://www.me.net/R"><span>Results</span></a></li> 
<li><a href="http://www.me.net/P"><span>Pictures</span></a></li> 
<li><a href="http://www.me.net/O.html"><span>Our Location</span></a></li>

现在我的页面上是这样做的(index.php):
<div id="tabs1" >
    <ul>
        <!-- CSS Tabs -->
        <?php include("menu.php"); ?>
    </ul>
</div>

所以,我想要做的是将上面那行改成这样:
<?php include("menu.php?current=pictures"); ?>

这会使活动选项卡成为“图片”选项卡。我该怎么做?

最好将 include("menu.php") 保留,然后在 index.php 中通过 URL 传递参数 ?current=pictures,再使用该变量。 - Anthony Forloney
5个回答

5
你也可以尝试这个:
你的php脚本
<?php
    $selected = "pictures";
    $current_id = ' id="current"';
    include "menu.php";
?>

这是您的菜单:
<ul>
<li <?php if ($selected == "pictures") print $current_id; ?>><a href="#"><span>Home</span></a></li> 
<li <?php if ($selected == "blog") print $current_id; ?>><a href="http://blog.me.net/"><span>Blog</span></a></li> 
<li <?php if ($selected == "home") print $current_id; ?>><a href="http://www.me.net/R"><span>Results</span></a></li> 
<li <?php if ($selected == "me") print $current_id; ?>><a href="http://www.me.net/P"><span>Pictures</span></a></li> 
<li <?php if ($selected == "contacts") print $current_id; ?>><a href="http://www.me.net/O.html"><span>Our Location</span></a></li>
</ul>

3

试试这个:

<li <?php if($_GET['current'] == 'home') {echo 'id="current"'}?>><a href="#"><span>Home</span></a></li> 
<li <?php if($_GET['current'] == 'blog') {echo 'id="current"'}?>><a href="http://blog.me.net/"><span>Blog</span></a></li> 
<li <?php if($_GET['current'] == 'results') {echo 'id="current"'}?>><a href="http://www.me.net/R"><span>Results</span></a></li></li>
and so on....

1
由于某些原因它没有起作用。我按照你说的完全做了。 - Bob Dylan
1
@Bob:你当前的值是来自查询字符串吗?例如当你访问博客页面时,像这样访问它:blog.php?current=blog - Sarfraz

2

0
<nav>
   <style>
      #active{
        color:#FFC801;
      }
   </style>
   <?php
      $activePage = basename($_SERVER['PHP_SELF'], ".php");
      ?>
   <ul>
      <li><a href="about.php" id="<?= ($activePage == 'about') ? 'active':''; ?>">About Us</a></li>
      <li><a href="mentors.php" id="<?= ($activePage == 'mentors') ? 'active':''; ?>">Mentors</a></li>
      <li><a href="tours.php" id="<?= ($activePage == 'tours') ? 'active':''; ?>">Tours</a></li>
      <li><a href="animation.php" id="<?= ($activePage == 'animation') ? 'active':''; ?>">Animation</a></li>
      <li><a href="blogs.php" id="<?= ($activePage == 'blogs') ? 'active':''; ?>">Blog</a></li>
      <li><a href="testimonials.php" id="<?= ($activePage == 'testimonials') ? 'active':''; ?>">Testimonials</a></li>
      <li><a href="press_media.php" id="<?= ($activePage == 'press_media') ? 'active':''; ?>">Press/Media</a></li>
      <li><a href="facts.php" id="<?= ($activePage == 'facts') ? 'active':''; ?>">Facts</a></li>
   </ul>
</nav>

请提供解释,说明您是如何解决所提出的问题的。 - Illegal Argument

-2

我认为没有必要在服务器端完成这个任务(使用CPU周期)。

可以使用JavaScript/CSS来实现它。


使用服务器端解决方案的一个原因是,一些浏览器(例如在慢速手机上运行的浏览器)可能会对JavaScript感到困难。 - Chris Dutrow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接