Jorge Garcia Jorge Garcia - 1 year ago 81
PHP Question

Category with class active or not in PHP

I have this.

<?php if (stripos($_SERVER['REQUEST_URI'], 'portfolio?cat=$cat_name')){ ?>
<li class="active" role="category"><a href="portfolio.php?cat=<?php echo $cat_name; ?>"><?php echo $cat_name; ?></a></li>
<?php } else { ?>
<li role="category"><a href="portfolio.php?cat=<?php echo $cat_name; ?>"><?php echo $cat_name; ?></a></li>
<?php } ?>

I just want to make the typical active category but getting the parameter.


Answer Source

First, Bert's comment about the single quotes, it will be the literal cat_name with a $ in front, not the value of $cat_name as you probably assume.

Also, why don't you just use:

if($_REQUEST['cat'] == $cat_name) { // ...

I mean, it already is a query param that ends up in $_REQUEST (unless you have weird server configuration, then it probably should be $_GET).

Also you don't distinguish between the two likely return values of stripos which are in your case 0 and false (the one meaning, the needle starts at position 0 of the haystack, and the other, that the needle wasn't found). So at the very least, you would have to stripos(...) !== false

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download