Jump to content
Sign in to follow this  
rootKID

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

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! :)

Share this post


Link to post
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"

Share this post


Link to post
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

Share this post


Link to post
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! :)

Share this post


Link to post
Share on other sites

Print $_SERVER['PHP_SELF'] to see what it's set to, use that to figure out what to check for.

Share this post


Link to post
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! :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...