Create a php menu that highlights current tab

2020-03-26 04:36发布

So I have a menu in a php file that looks like this (This is the whole file. I'm totally new to 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>

Now in my pages I do this (index.php):

<div id="tabs1" >
    <ul>
        <!-- CSS Tabs -->
        <?php include("menu.php"); ?>
    </ul>
</div>

So what I want to be able to do is change the line above to this:

<?php include("menu.php?current=pictures"); ?>

Which would make the active tab the Pictures tab. How can I do this?

标签: php html css menu
5条回答
Rolldiameter
2楼-- · 2020-03-26 05:19

Try this:

<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....
查看更多
够拽才男人
3楼-- · 2020-03-26 05:19

I don't think its necessary for it to be done at the server side (using up CPU cycles).

Use javascript/CSS to achieve this.

查看更多
狗以群分
4楼-- · 2020-03-26 05:24
<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>
查看更多
Melony?
5楼-- · 2020-03-26 05:26
Viruses.
6楼-- · 2020-03-26 05:27

You could also try this:

Your php script

<?php
    $selected = "pictures";
    $current_id = ' id="current"';
    include "menu.php";
?>

this is your menu:

<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>
查看更多
登录 后发表回答