BritishSam
BritishSam

Reputation: 1080

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

Upvotes: 0

Views: 1547

Answers (2)

Ripa Saha
Ripa Saha

Reputation: 2540

<?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'].

Upvotes: 0

Rednas
Rednas

Reputation: 609

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

Upvotes: 2

Related Questions