Fairly new to CodeIgniter, still grasping the MVC approach. I\'m just wondering what\'s the best way to solve this:
I got my navigation bar highlighting the currentl
I saw from the comment made above that perhaps you were looking for something a little bit more compact. There isn't a function like that in Codeigniter by default, but it's easy enough to make one. This is just something I put together as of now, but it might be what you want to have.
First, take a look at the URL helper in the manual. http://codeigniter.com/user_guide/helpers/url_helper.html
Look specifically at the anchor function and understand how to use. You should be in the habit to use this helper, the HTML helper and the form helper. It will improve your views. All the helpers are available for us in the system folder. What I did was I just opened up the URL helper and copied the anchor code, I then created my own helper file in the helper folder under application. You can read more about creating your own helpers and autoloading them in the manual.
I then made a few changes to it. The final result looks like this:
if ( ! function_exists('menu_anchor'))
{
function menu_anchor($uri = '', $title = '', $attributes = '')
{
$title = (string) $title;
if ( ! is_array($uri))
{
$site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
}
else
{
$site_url = site_url($uri);
}
if ($title == '')
{
$title = $site_url;
}
if ($attributes != '')
{
$attributes = _parse_attributes($attributes);
}
$attributes .= ($site_url == current_url()) ? ' selected' : '';
return ''.$title.'';
}
}
As you can see, it's only a one line modification: $attributes .= ($site_url == current_url()) ? ' selected' : '';
Basically, if the URL of the retrieved page matches the papge that the anchor links to, it adds the class selected. If you don't want the selected link to link anywhere specific, you can easily fix this as well by setting $site_url to # or something else.