navigation - php nav bar active

41

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

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

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

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

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

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

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

    echo ($PHP_SELF == '/contact.php') ?
    '<li ><a href="contact.php">Contact</a></li>' :
    '<li ><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

81

Answer

Solution:

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

288

Answer

Solution:

<?php 
      echo '<ul >';

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

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

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

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

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

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

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

      echo '</ul>';
      ?>

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

People are also looking for solutions to the problem: Substituting whitespaces with %20 in PHP. urlencode and rawurlencode does not work

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.