kabeersvohra
kabeersvohra

Reputation: 1059

How to set li to active in php

I have my navbar in a file called navbar.php which I include in each of the indiviual pages. I want to know how to add the active class to the button of the page that is selected using php. I have seen many tutorials of using jquery but since this happens on the page load there must be a solution without having to use jquery/javascript etc. So for example I have a line:

<li id="aboutus"><a href="#">About us</a></li>

I want there to be a line of code in aboutus.php which can change this earlier php code to

<li id="aboutus" class="active"><a href="#">About us</a></li>

Thanks

Upvotes: 0

Views: 106

Answers (3)

HarrieDakpan
HarrieDakpan

Reputation: 90

I hope this is what you're looking for. It gets the page you're currently on. Afterwards it checks if the page is aboutus and then it echo active.

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


    <li id="aboutus" class="<?php if($pg == 'aboutus.php'){echo 'active';} ?>"><a href="#">About us</a></li>

Upvotes: 2

soundhiraraj
soundhiraraj

Reputation: 323

Try like this,it works for me,

   <li <?php if($id == your_id){ echo "class='nav visited'";} ?> >

Upvotes: 1

Gounemond
Gounemond

Reputation: 353

If you want it in php you can just echo it

<?php 
    echo '<li id="aboutus" class="active"><a href="#">'.$yourString.'</a></li>';
?>

The file extension will be .phtml!

Upvotes: 1

Related Questions