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>
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>
<li class="<?php echo ($_GET['page'] == 'about.php')? 'current' : 'normal'; ?>">
<a href="index.php?page=about.php">About</a>
</li>
$_SERVER['PHP_SELF']
doesn't return the $_GET vars (?page=about.php
)
$_SERVER['PHP_SELF']
will start with /
and will not include the query string.
You should probably check $_GET['page']
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With