anon
anon

Reputation:

show same page with different menu item active

I have these 2 menu and sub-menus for my help manual which has a page leftmenu.php where I have all the menu's and I have included this file above all my pages.

<li>
    <a href=""><i class="fa fa-briefcase"></i> <span class="nav-label">Main menu 1 </span><span class="fa arrow"></span></a>
    <ul class="nav nav-second-level collapse">
        <li><a href="project.php">Project Management</a></li>
        <li><a href="task.php">Task Management</a></li>
        <li><a href="defect.php">Defect Management</a></li>
        <li><a href="customer1.php">Customer Management</a></li>
        <li><a href="user.php">User Management</a></li>
        <li><a href="settings.php">Settings</a></li>
    </ul>
</li>
<li>
    <a href=""><i class="fa fa-ticket"></i> <span class="nav-label">Main menu 2 </span><span class="fa arrow"></span></a>
    <ul class="nav nav-second-level collapse">
        <li><a href="ticket.php">Ticket Management</a></li>
        <li><a href="resolution.php">Resolutions</a></li>
        <li><a href="customer.php">Customer Management</a></li>
        <li><a href="user.php">User Management</a></li>
        <li><a href="settings.php">Settings</a></li>
    </ul>
</li>

Now I have this one customer.php which I would like to use in both the menu's but the menu from where Customer Management is clicked should only be active. How to I achieve this as I am using only php and html for this. Thankyou

My code now:

<li <?= active('leads|user|settings')?>>
                    <a href=""><i class="fa fa-diamond"></i> <span class="nav-label">Main menu1</span> <span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">
                        <li <?= active('leads') ?>><a href="leads.php">Lead Management</a></li>
                        <li <?= active('user') ?>><a href="user.php">User Management</a></li>
                        <li <?= active('settings') ?>><a href="settings.php">Settings</a></li>
                    </ul>
                </li>

                <li <?= active('project|task|defect|customer1|users|settings1')?>>
                    <a href=""><i class="fa fa-briefcase"></i> <span class="nav-label">Main menu2 </span><span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level collapse">
                      <li <?= active('project') ?>><a href="project.php">Project Management</a></li>
                        <li <?= active('task') ?>><a href="task.php">Task Management</a></li>
                        <li <?= active('defect') ?>><a href="defect.php">Defect Management</a></li>
                        <li <?= active('customer1') ?>><a href="customer1.php">Customer Management</a></li>
                        <li <?= active('users') ?>><a href="users.php">User Management</a></li>
                        <li <?= active('settings1') ?>><a href="settings1.php">Settings</a></li>
                    </ul>
                </li>

Upvotes: 1

Views: 299

Answers (2)

Thielicious
Thielicious

Reputation: 4442

function active($page) {
    if (preg_match("/$page\./",$_SERVER["PHP_SELF"])) {
        echo "class=active";
    }
}

Example

<li><a href=index.php <?= active('index') ?>>Home</a></li>

EDIT:
And Example for Main Menu 1

<a <?= active('project|task|defect|customer1|user|settings') ?> href=""><i class="fa fa-briefcase"></i> <span class="nav-label">Main menu 1 </span><span class="fa arrow"></span></a>

Upvotes: 1

Tom
Tom

Reputation: 399

I would wrap it in a php if statement like this for each value you want to show

if ($_SERVER['REQUEST_URI'] == "my/url/") {
// do something
} else {
// do something else
}

Upvotes: 0

Related Questions