4

What's wrong with the following code? I would like to dynamically insert "current" CSS class when the respective <li> element is clicked. Thanks!

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

<section>
    <aside>       
        <nav>
            <ul>
                <li class="<?php if($pg == 'index.php?page=about.php'){echo 'current';} ?>"><a href="index.php?page=about.php">About</a></li>                       
            <li class=""><a href="">Services</a></li> 
            </ul>
        </nav>
    </aside>
<section>
5
  • That's an HTML class. There is no such thing as a CSS class. The term sometimes gets used to mean "rule-set", "class selector", "any selector" and "property". This is confusing, so please don't use the term. Commented May 31, 2011 at 12:50
  • Do you mean that you want to add to the element a class when the user clicks on it, i.e. on the client side? Commented May 31, 2011 at 12:50
  • 1
    The syntax looks fine, I would echo out $pg onto the page, it's possible that it is not exactly what you are expecting, that way you can what your condition needs to be. Commented May 31, 2011 at 12:52
  • 3
    $_SERVER['PHP_SELF'] returns the full working dir (everything after /public_html/) Commented May 31, 2011 at 12:53
  • you can try this, Add if statement and assign value to few variables/ or one variable. then inside html code, add php variable into class section, Ex- <div class="<?php $myvar; ?> other-class" > Commented Oct 18, 2017 at 10:57

5 Answers 5

11

Try with:

<?php 
$pg = $_GET['page'];

$allow = array('about.php', 'main.php', 'other.php');
if ( !in_array($pg, $allow) ) {
    $pg = 'default_value';
}
?>

<section>
    <aside>       
        <nav>
            <ul>
                <li class="<?php if($pg == 'about.php'){echo 'current';} ?>"><a href="index.php?page=about.php">About</a></li>                       
            <li class=""><a href="">Services</a></li> 
            </ul>
        </nav>
    </aside>
<section>
Sign up to request clarification or add additional context in comments.

2 Comments

+1 But you could also explain him the security flaws his method possibly has.
You could use the most recent syntax for arrays instead of $maybe = array(...) you can use $yes = [...]
2
<li class="<?php echo ($_GET['page'] == 'about.php')? 'current' : 'normal'; ?>">
  <a href="index.php?page=about.php">About</a>
</li>

Comments

1

$_SERVER['PHP_SELF'] doesn't return the $_GET vars (?page=about.php)

Comments

1

$_SERVER['PHP_SELF'] will start with / and will not include the query string.

You should probably check $_GET['page'] instead.

Comments

0

Well if your looking for clicked at runtime you need javascript not php. Something like this using jQuery will work for you:

$(document).ready(function(){
    $('nav >ul li').click(function(){
        //remove all current classes
        $('.current').removeClass('current');
        $(this).addClass('current');
    });
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.