Adrian
Adrian

Reputation: 2012

highlight current page link in opencart or any php

Im just learning open cart, but I think this question could be answered by anyone with good php knowledge.

I am simply attempting to highlight the a link when only on that page, but it doesnt seem to work

<?php $tickets = 'index.php?route=product/category&path=600'; ?>
<ul>
    <li><a href="http://www.limerickfc.ie">Limerick FC</a></li>
    <li><a href="<?php echo $tickets; ?>" <?php
if (strpos($_SERVER['PHP_SELF'], $tickets )) echo "class=\"current\" ";
?>  > Tickets     </a></li>
    <li><a href="<?php echo $home; ?>" class="current">Shop</a></li>
</ul>

I know the variable $tickets is fine because the link goes to where its supposed to go, and I know the class current is fine because it works for the third li which is shop.

Am I using strpos correctly?

Upvotes: 0

Views: 2344

Answers (2)

Muhammad Bilal
Muhammad Bilal

Reputation: 449

I think you should use basename($_SERVER['REQUEST_URI']);.

<a href="<?php echo $tickets; ?>" <?php echo (basename($_SERVER['REQUEST_URI']) == $tickets) ? '"class=\"current\"' : ""; ?>>Tickets</a>

it would be more better if you declare basename($_SERVER['REQUEST_URI']) in variable.

Upvotes: 1

Stann0rz
Stann0rz

Reputation: 667

strpos() returns false if the string matches at char 0 - which the above does.

Use indentical comparisons === for a true outcome.

As a note, i've used substr_count() in the past for similar circumstances without needing identical comparison!

Upvotes: 0

Related Questions