Jump to content

how to put "acivte" class in navbar when browsing specific file?


rootKID

Recommended Posts

Hello W3S. It has been a while :)

I remember i once asked about this and one of you came up with a solution i could try. I have this file "navbar.php" which is included to all my page files, it is a global navbar. How ever i have forgotten how to do this and i lost my previous work on one of my HDD.

I would like to know how to add the class ".active" when viewing index.php and when seeing blog.php of course, that is the one active and not index, and so on.

I remember last time, you guys told me that i could check if i was on a specific file, and if so, i could add it and if not, nothing would happen, any ideas? I remember this was done with if/else statements...

and yes i know i can make use of the url with variables, passing it through there but i am trying to avoid this so the url are... well.. pretty without it :P

Hope you can help me out!

Thanks a lot in advance! :)

Link to comment
Share on other sites

You might be able to query from what file the request is running on.

You can always use $_SERVER['PHP_SELF'] to find out what file is running. Just go something like this for a quick and dirty solution.

<li class="<?php echo ($_SERVER['PHP_SELF'] == "/index.php" ? "active" : "");?>">
<a href="index.php">Start</a>
</li>
<li class="<?php echo ($_SERVER['PHP_SELF'] == "/about.php" ? "active" : "");?>">
<a href="about.php">About</a>
</li>
<li class="<?php echo ($_SERVER['PHP_SELF'] == "/practices.php" ? "active" : "");?>">
<a href="practices.php">Practices</a>
</li>

From what I've seen, it uses a relative-to-root path so if its under any directories you'll need to write it as "/sub-directory/about.php"

Link to comment
Share on other sites

ahh, ok i will test it and get back to you! Thanks!

One question. If i use the PHP_SELF... does it give the "index.php" or just the "index" name? Thanks in advance tho! :D

Link to comment
Share on other sites

ahh, ok i will test it and get back to you! Thanks!

One question. If i use the PHP_SELF... does it give the "index.php" or just the "index" name? Thanks in advance tho! :D

EDIT:

Okay so i tested it and it does not work properly, maybe me doing it wrong.

I am including the file "navbar.php" from location "include/html/GLOBAL/navbar/navbar.php" How should i put it in? If that is the problem that is?

Thanks! :)

Link to comment
Share on other sites

how about that.. it worked. Feeling like a rookie still... after so long :P

It was because i needed "root" in front. I have placed my project in a sub-folder called root. lol :P

Thanks a lot! Working now as i can see it! :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...