Reputation: 1009
Im doing the active in sidebar. After the admin logged in(ofcourse it will automatically redirect to localhost:/xxx/Admin/index or localhost:/xxx/Admin/) and the active is in the dashboard. When the admin clicked the user management(it will redirect to localhost:/xxx/Admin/UserAccount) then the active will be change into the user management/.. The question is, is it possible to make an if statement, when the specific URL is that/this?
View
<li class="<?= highlight_menu('Administrator/userAccount',TRUE)?>treeview">
<a href="#">
<i class="fa fa-users"></i> <span>User Management</span>
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
<ul class="treeview-menu">
<li><a href="<?= base_url(). 'Administrator/userAccount'?>"><i class="fa fa-circle-o"></i> View users account</a></li>
</ul>
</li>
Common Helper
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function highlight_menu($menu_item, $has_routes = FALSE)
{
$CI = get_instance();
if(! $has_routes) // activate only if the exact URL is found
return (uri_string()==$menu_item) ? 'active-menu':'';
$controller = strtolower($CI->router->fetch_class());// get controller name
$method = strtolower($CI->router->fetch_method()); // get method name
return (strtolower($menu_item) == $controller."/".$method) ? 'active-menu' : ''; // activate only if the controller and method matches
}
?>
Upvotes: 0
Views: 77
Reputation: 1599
The following lines of code will help you to highlight a menu item depending on the current URL. For simplicity and re-usability I am adding this as a helper function into my helper file. (I already have a helper file added at application/helpers/common_helper.php and auto-loading it when the application starts)
Read more about helper functions here
https://www.codeigniter.com/userguide3/general/helpers.html
function highlight_menu($menu_item, $has_routes = FALSE)
{
$CI = get_instance();
if(! $has_routes) // activate only if the exact URL is found
return (uri_string()==$menu_item) ? 'active-menu':'';
$controller = strtolower($CI->router->fetch_class());// get controller name
$method = strtolower($CI->router->fetch_method()); // get method name
return (strtolower($menu_item) == $controller."/".$method) ? 'active-menu' : ''; // activate only if the controller and method matches
}
Then in your view, add the menu items something like
<ul>
<li class="menu_item">
<a class="menu-class <?php echo highlight_menu('controller/method_name', TRUE);?>" href="<?php echo base_url('controller/method_name');?>">Menu Item</a>
</li>
</ul>
and in you CSS, use .active-menu
selector to add style for the active menu item.
Upvotes: 1