php导航栏活跃

时间:2013-03-05 11:36:50

标签: php navigation

您好我试图让我的导航栏按钮处于活动状态,因此当它在该特定页面上时它看起来不同。

当我手动将我的li类更改为活动状态时,它可以正常工作,但是当我使用下面的代码执行此操作时,该类保持不动。

<?php 
    echo '<ul class="nav">';

        echo ($PHP_SELF == '/index.php') ?
        '<li class="active"><a href="index.php">Home</a></li>' :
        '<li class="none"><a href="index.php">Home</a></li>';

        echo ($PHP_SELF == '/how-it-works.php') ?
        '<li class="active"><a href="how-it-works.php">How it works</a></li>' :
        '<li class="none"><a href="how-it-works.php">How it works</a></li>';

        echo ($PHP_SELF == '/gas.php') ?
        '<li class="active"><a href="gas.php">Gas</a></li>' :
        '<li class="none>"><a href="gas.php">Gas</a></li>';

        echo ($PHP_SELF == '/electric.php') ?
        '<li class="active"><a href="electric.php">Electric</a></li>' :
        '<li class="none"><a href="electric.php">Electric</a></li>';

        echo ($PHP_SELF == '/telecoms.php') ?
        '<li class="active"><a href="telecoms.php">Telecoms</a></li>' :
        '<li class="none>"><a href="telecoms.php">Telecoms</a></li>';

        echo ($PHP_SELF == '/services.php') ?
        '<li class="active"><a href="services.php">Services</a></li>' :
        '<li class="none"><a href="services.php">Services</a></li>';

        echo ($PHP_SELF == '/contact.php') ?
        '<li class="active"><a href="contact.php">Contact</a></li>' :
        '<li class="none"><a href="contact.php">Contact</a></li>';

         echo '</ul>';
?>

所以我想要的是当我在这个类的索引页面上激活时,当我不在索引页面上时我希望这个类是无

2 个答案:

答案 0 :(得分:2)

尝试使用PHP http://php.net/manual/en/reserved.variables.server.php

的服务器变量
$_SERVER['PHP_SELF']

这将起作用我认为

答案 1 :(得分:0)

<?php 
            echo '<ul class="nav">';

                echo ($_SERVER['PHP_SELF'] == 'index.php') ?
                '<li class="active"><a href="index.php">Home</a></li>' :
                '<li class="none"><a href="index.php">Home</a></li>';

                echo ($_SERVER['PHP_SELF'] == 'how-it-works.php') ?
                '<li class="active"><a href="how-it-works.php">How it works</a></li>' :
                '<li class="none"><a href="how-it-works.php">How it works</a></li>';

                echo ($_SERVER['PHP_SELF'] == 'gas.php') ?
                '<li class="active"><a href="gas.php">Gas</a></li>' :
                '<li class="none>"><a href="gas.php">Gas</a></li>';

                echo ($_SERVER['PHP_SELF'] == 'electric.php') ?
                '<li class="active"><a href="electric.php">Electric</a></li>' :
                '<li class="none"><a href="electric.php">Electric</a></li>';

                echo ($_SERVER['PHP_SELF'] == 'telecoms.php') ?
                '<li class="active"><a href="telecoms.php">Telecoms</a></li>' :
                '<li class="none>"><a href="telecoms.php">Telecoms</a></li>';

                echo ($_SERVER['PHP_SELF'] == 'services.php') ?
                '<li class="active"><a href="services.php">Services</a></li>' :
                '<li class="none"><a href="services.php">Services</a></li>';

                echo ($_SERVER['PHP_SELF'] == 'contact.php') ?
                '<li class="active"><a href="contact.php">Contact</a></li>' :
                '<li class="none"><a href="contact.php">Contact</a></li>';

            echo '</ul>';
            ?>

删除所有“/”并使用$ _SERVER ['PHP_SELF']。