Grow your CSS skills. Land your dream job.

[Solved] PHP to add conditional css classes?

  • # November 23, 2009 at 1:29 pm

    Hi,
    I am using the jquery lavalamp plugin for a piece of navigation, and the plugin requires that <li> for the active page must have a class of "current"… and that is what makes it highlight the appropriate page. Thats all fine and dandy…

    My problem is that for my navigation, I have one php file with all my navigation that the rest of my pages link to using php insert. So just setting one <li> as current would highlight only one navigation option throughout all the pages.

    What im wondering is if theres a way to have a php statement that says that "if the body id="blabla", then put "current" in li class. If not then dont do anything."

    Code:

    This way, for example, on the home page it will see that my body id = "home" and then insert the "current" class into the <li> tag for the home link, and that one will be highlighted on the home page. And when the body id ="about", it will highlight the about link…. etc.

    Thanks so much… any help will be greatly appreciated. At least if someone can tell me if this is even possible or not.

    # November 23, 2009 at 1:39 pm

    This sounds like you are working on a custom-programmed site.

    Yes it’s possible, but you need to somehow tell your navigation file what page you are on.

    Here’s one example I’ve done:

    Code:
    < ?
    $pg = $_SERVER['PHP_SELF'];
    ?>

Each If statement checks to see if $pg == the page name, and then echo’s current into the class if so.

Hope that helps.

# November 23, 2009 at 1:56 pm

thanks! … now should i put

Code:
< ?
$pg = $_SERVER['PHP_SELF'];
?>

in my navigation php file, or in each of my actual page files?

heres what my navigation file looks like:

Code:
< ?
$pg = $_SERVER['PHP_SELF'];
?>

I must be doing something wrong, because its not catching it. Here is the link to the test site:
http://mizirawi.com/justinmizirawi/index.php

The only working links are home and about… it defaults to home. But when i go to the about page… it should highlight that link

# November 23, 2009 at 2:47 pm

All fixed…. i just needed to put the folder name before the actual file so PHP_SELF could recognize it. Thanks for all your help AshtonSanders! :D

# November 23, 2009 at 3:35 pm

Awesome. Glad it worked.[img]http://www.websitesinaflash.com/images/thumbsup.gif[/img]

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".