简体   繁体   中英

php nav bar active

Hi im trying to make my nav bar button active so it looks different when it is on that specific page.

when i manually change my li class to active it works but when i use my code below to do it the class stays at none.

<?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>';
?>

so what i want is when im on the index page for the class to be active, and when im not on the index page i want the class to be none

try to use the server variable of PHP http://php.net/manual/en/reserved.variables.server.php

$_SERVER['PHP_SELF']

that will work i think

<?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>';
            ?>

remove all "/" and use $_SERVER['PHP_SELF'].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM