2010-02-07 46 views
3

所以我有一個PHP文件看起來像這樣的菜單創建一個PHP菜單,突出當前標籤頁

menu.php(這是整個文件我是全新到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"); ?> 

這將使活動選項卡圖片選項卡。我怎樣才能做到這一點?

+0

離開'include(「menu.php」);'然後在'index.php'通過URL傳遞參數'?current = pictures'然後使用變量可能會更好? – 2010-02-07 08:59:53

回答

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

由於某種原因,這不起作用。我完全像你說的那樣。 – 2010-02-07 09:02:30

+1

@Bob:你的當前值來自查詢字符串,例如當你訪問博客頁面,像這樣訪問它blog.php?current = blog – Sarfraz 2010-02-07 09:40:51

-1

我不認爲它必要爲它在服務器端進行(使用CPU週期)。

使用javascript/CSS來實現這一點。

+0

使用服務器端解決方案的一個原因是一些瀏覽器,比如那些運行慢手機可能會與JavaScript鬥爭。 – 2015-12-05 00:54:38

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> 
+0

請提供解釋如何解決問題的解釋。 – 2017-01-14 05:51:40