12

Dynamic Header, CSS Class Change To Active USING PHP (dirrectory)

I want the class of the <li> tag to change under the active dirrectory... now, every guide shows me how to do it when your page equals it, but i want to change the <li> depending on what dirrectory im on

for example:

if say im on
http://example.com/RESOURCES/code/opensource,
or
http://example.com/RESOURCES/images/clipart
i want the "RESOURCES" ^^ <li> to be 'class="active"' while the rest display 'class="noactive"'

or if im on
http://example.com/tutorials/css/flawless-dropdown-menu
I want the "tutorials" <li> to be 'class="active"' while the rest are 'class="noactive"'


URL Setup:

This is my example of how my url's are displayed...

http://example.com/tutorials/css/flawless-dropdown-menu

^^That URL is the page of a tutorial....under the "tutorials" directory, than under the "CSS" category directory, than the page title (all of these directories are not real and are rewrites from .htaccess) [irrelevant]


Navigation Setup:

<ul id="mainnav">
  <li class="noactive"><a href="/">Home</a></li>
  <li class="active"><a href="/tutorials/">Tutorials</a></li>
  <li class="noactive"><a href="/resources/">Resources</a></li>
  <li class="noactive"><a href="/library/">Library</a></li>
  <li class="noactive"><a href="/our-projects/">Our Projects</a></li>
  <li class="noactive"><a href="/community/">Community</a></li>
</ul>
Yes Barry
  • 9,514
  • 5
  • 50
  • 69
Alex Sarnowski
  • 749
  • 1
  • 11
  • 21
  • Is the part you need to be active always the first segment? – Rick Calder Nov 11 '12 at 22:41
  • if you want flexible code, you have to add your real URL after translate via rewrite code. however i suggest to make it using javascript or jquery. – Mohammad Ahmad Nov 11 '12 at 22:43
  • No, It all depends on what directory im in, regardless if how many layers of dirrectories i am in deep, i just want it to look for what dirrectory im in (first layer) – Alex Sarnowski Nov 11 '12 at 22:48

14 Answers14

23

Figured out the ANSWER...I was over thinking it.

HTML

<ul id="mainnav">
    <li class="<?php if ($first_part=="") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Home</a></li>
    <li class="<?php if ($first_part=="tutorials") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Tutorials</a></li>
    <li class="<?php if ($first_part=="resources") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Resources</a></li>
    <li class="<?php if ($first_part=="library") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Library</a></li>
    <li class="<?php if ($first_part=="our-projects") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Our Projects</a></li>
    <li class="<?php if ($first_part=="community") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Community</a></li>
</ul>

PHP

<?php 
$directoryURI = $_SERVER['REQUEST_URI'];
$path = parse_url($directoryURI, PHP_URL_PATH);
$components = explode('/', $path);
$first_part = $components[1];
?>
Alex Sarnowski
  • 749
  • 1
  • 11
  • 21
23

header.php

$activePage = basename($_SERVER['PHP_SELF'], ".php");

nav.php

<ul>
    <li class="<?= ($activePage == 'index') ? 'active':''; ?>"><a href="/index.php">Home</a></li>
    <li class="<?= ($activePage == 'tutorials') ? 'active':''; ?>"><a href="/tutorials.php">Tutorials</a></li>
...
vlycser
  • 408
  • 3
  • 7
7

Through PHP you can try -

<?php 
// gets the current URI, remove the left / and then everything after the / on the right
$directory = explode('/',ltrim($_SERVER['REQUEST_URI'],'/'));

// loop through each directory, check against the known directories, and add class   
$directories = array("index", "tutorials","resources","library","our-projects","community"); // set home as 'index', but can be changed based of the home uri
foreach ($directories as $folder){
$active[$folder] = ($directory[0] == $folder)? "active":"noactive";
}
?>

<ul>
  <li class="<?php echo $active['index']?>"><a href="/">Home</a></li>
  <li class="<?php echo $active['tutorials']?>"><a href="/tutorials/">Tutorials</a></li>
  <li class="<?php echo $active['resources']?>"><a href="/resources/">Resources</a></li>
  <li class="<?php echo $active['library']?>"><a href="/library/">Library</a></li>
  <li class="<?php echo $active['our-projects']?>"><a href="/our-projects/">Our Projects</a></li>
  <li class="<?php echo $active['community']?>"><a href="/community/">Community</a></li>
</ul>
Sean
  • 12,443
  • 3
  • 29
  • 47
  • 1
    @RickCalder I disagree, your answer is better. – Yes Barry Nov 11 '12 at 23:23
  • Everything works here, besides the url dirrectory being recognized and applied to the code...but the whole, active, noactive class establishing works. Im just not sure why it isn't recognizing directory – Alex Sarnowski Nov 11 '12 at 23:40
  • @AlexSarnowski are you saying that `$directory = explode('/',ltrim($_SERVER['REQUEST_URI'],'/'));` is not working? ie on `http://example.com/tutorials/css/flawless-dropdown-menu` that `$directory != tutorials`? Could you echo `$directory` to see what it is set to, compared to the directory you are in? Could it a `case-sensitive` issue? – Sean Nov 12 '12 at 00:09
  • I tried to echo it out and it says "Notice : Array to string conversion in " – Alex Sarnowski Nov 12 '12 at 00:36
  • And yes, i'm saying when i'm on http://example.com/tutorials/css/flawless-dropdown-menu , all of the
  • are class="noactive" , when tutorials should be class="active"
  • – Alex Sarnowski Nov 12 '12 at 00:47
  • The warning - "Notice : Array to string conversion in " is caused when you try to manipulate a string when it is actually an array. For example if you had `ltrim($_SERVER,'/')`[array], instead of `ltrim($_SERVER['REQUEST_URI'],'/')`[string]. Try doing `print_r($_SERVER['REQUEST_URI'])` and see if prints as `ARRAY ...` or as a string. You could also do it with `print_r($directory)` and `print_r($directory[0])` to check if they are correct and match the directory in your url. – Sean Nov 12 '12 at 02:37