Active link in the navigation list

I have a separate file (LIB.php) with a specific function. This function is called on every page. I want to make it dynamic, when I go to one of the pages, the page becomes active in

function get_Navigation(){

$navigation = <<<END
 <div class="menubar">

    <ul>
      <li><a href="index.php"><i class="icon-home icon-large"></i><main>Home</main></a></li>
      <li><a href="admin.php"><i class="icon-star icon-large"></i><main>Admin</main></a></li>
      <li><a href="cart.php"><i class="icon-money icon-large"></i><main>Cart</main></a></li>
    </ul>

  </div>
END;

echo $navigation;
}

      

+3


source to share


2 answers


Use basename($_SERVER['PHP_SELF']);

to get the filename, this will return something like index.php

, then you can add a class to the string or link to change its appearance. So your code might look something like this if you want to keep using the heredoc chaining instead of concatenating multiple lines:

$isactive = "isactive";
function isactive($filename){
    $currentfile = basename($_SERVER['PHP_SELF']);
    if($currentfile == $filename) return "active";
}
$navigation = <<<END
 <div class="menubar">

    <ul>
      <li class='{$isactive("prog.php")}'><a href="index.php"><i class="icon-home icon-large"></i><main>Home</main></a></li>
      <li class='{$isactive("admin.php")}'><a href="admin.php"><i class="icon-star icon-large"></i><main>Admin</main></a></li>
      <li class='{$isactive("cart.php")}'><a href="cart.php"><i class="icon-money icon-large"></i><main>Cart</main></a></li>
    </ul>

  </div>
END;

echo $navigation;

      



Note that I am creating a variable $isactive

which may seem like overkill, but this is a way of tricking the heredoc line to return the value of the function instead of just typing my name, but this is really in a different scope.

+1


source


It depends if you are using some kind of framework and rewriting urls. If we assume its simple plain PHP without rewriting, you can do something like:

function get_Navigation(){

   $current_filename = basename(__FILE__, ".php");
   $menuItems = array('index' => false, 'admin' => false, 'cart' => false);

   switch($current_filename) {
      case 'index': $menuItems['index'] = true; break;
      case 'admin': $menuItems['admin'] = true; break;
      case 'cart': $menuItems['cart'] = true; break;
   }

   $navigation = '
      <div class="menubar">
       <ul>
         <li><a href="index.php" class="' . ($menuItems['index'] ? 'selected' : '') . '"><i class="icon-home icon-large"></i><main>Home</main></a></li>
         <li><a href="admin.php" class="' . ($menuItems['admin'] ? 'selected' : '') . '"><i class="icon-star icon-large"></i><main>Admin</main></a></li>
         <li><a href="cart.php" class="' . ($menuItems['cart'] ? 'selected' : '') . '"><i class="icon-money icon-large"></i><main>Cart</main></a></li>
       </ul>
  </div>
';

   echo $navigation;
}

      



I am assuming the index page is called index.php, the admin page is admin.php and the cart page is cart.php

This will add the 'selected' class to the active link.

0


source







All Articles