rootKID Posted February 17, 2019 Share Posted February 17, 2019 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 More sharing options...
Funce Posted February 17, 2019 Share Posted February 17, 2019 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 More sharing options...
rootKID Posted February 22, 2019 Author Share Posted February 22, 2019 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 More sharing options...
rootKID Posted February 22, 2019 Author Share Posted February 22, 2019 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! 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 More sharing options...
justsomeguy Posted February 22, 2019 Share Posted February 22, 2019 Print $_SERVER['PHP_SELF'] to see what it's set to, use that to figure out what to check for. Link to comment Share on other sites More sharing options...
rootKID Posted February 23, 2019 Author Share Posted February 23, 2019 how about that.. it worked. Feeling like a rookie still... after so long It was because i needed "root" in front. I have placed my project in a sub-folder called root. lol Thanks a lot! Working now as i can see it! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now