开发者

Php get current active class for menu

May i know how set 开发者_Go百科active menu which page something like page.php?name=about

$a = basename($_SERVER['SCRIPT_NAME']);

<li<?php if ($a == 'page.php?name=about'){?> class="active"<?php }?>><a href="<?php echo $url; ?>/page.php?name=about" title="About">About</a></li>

My class won't active while on page.php?name=about

May I know how to fix it?


you're looking for:

$_GET['name']

The $_GET superglobal is how you access variables from the query string.

$class = ($_GET['name'] == 'about' ? "active" : "");
echo "<li class=\"$class\">";


Change the if condition to: if ($a == 'page.php' && $_GET['name'] == 'about')


$_SERVER['SCRIPT_NAME'] only gives you the name of the script. The parameters aren't included. You need to use the $_GET array to get parameters (or $_POST if they came that way). Try this:

<li<?php if ($_GET['name'] == 'about'){?> class="active"<?php }?>><a href="<?php echo $url; ?>/page.php?name=about" title="About">About</a></li>


Your $a definition would need to be as follows to work as you intend

$a = $_SERVER['SCRIPT_NAME'].'?name='.$_GET['name'];
<li<?php if ($a == '/page.php?name=about'){?> class="active"<?php }?>><a href="<?php echo $url; ?>/page.php?name=about" title="About">About</a></li>

Alternatively, you can just use

if ($_SERVER['SCRIPT_NAME']=='/page.php')
    $a=$_GET['name'];

<li<?php if ($a == 'about'){?> class="active"<?php }?>><a href="<?php echo $url; ?>/page.php?name=about" title="About">About</a></li>


Try this...

<li><a href="page.php" 
    <?php if(strpos(basename($_SERVER['REQUEST_URI']),'page.php')=='page.php'){
        echo' class="active"';} 
    ?> ><!-- end of opening 'a' element -->
    About</a></li>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜