Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8476891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:18:14+00:00 2026-06-10T18:18:14+00:00

In a site navigation bar, I have some logic that determines which page the

  • 0

In a site navigation bar, I have some logic that determines which page the user is currently viewing, and gives that menu-item an .active class.

The code I came up with does work, though it doesn’t feel very elegant. Can you think of a way to do this better? How could I move the logic out of the view and into the controller? How can I stop repeating myself?

The controller:

<?php

class Pages extends CI_Controller {

    public function view ($page = 'home') {

        if (!file_exists('application/views/pages/'.$page.'.php'))
            show_404();

        $data['active'] = $page;

        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);

    }
}

?>

The view:

<ul class="nav">
    <li class="<?php echo($active=='home')?'active':''; ?>"><a href="./">blog</a></li>
    <li class="<?php echo($active=='about')?'active':''; ?>"><a href="about">about</a></li>
    <li class="<?php echo($active=='projects')?'active':''; ?>"><a href="projects">projects</a></li>
    <li class="<?php echo($active=='lab')?'active':''; ?>"><a href="lab">lab</a></li>
    <li class="<?php echo($active=='contact')?'active':''; ?>"><a href="contact">contact</a></li>
</ul>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:18:16+00:00Added an answer on June 10, 2026 at 6:18 pm

    Maybe more elegant ?

        function is_active($item) {
            return strpos(uri_string(), $item)!==FALSE ? 'active':'';
        }
    
    
        <li class="<?=is_active('blog')?>"><a href="./">blog</a></li>
    

    This way depend of complexity of your uris.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document that matches our site navigation something like this: <page
We have a site where the user gets a navigation bar to allow navigation
I have a site that has a scrolling navigation bar. I need to save
Is it possible to have a menu bar (navigation bar) on a web site
I have this code that is my navigation bar on my site, it is
ASP.NET v2 I have MasterPage which includes the navigation bar for the site along
I have an MVC 3 site. I've created a navigation bar that looks like
I have a navigation bar that I include using SSI in every page of
I have a sticky navigation bar on my site that needs to snap back
I have a Header/navigation bar on a site, it has a logo, navigation menu,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.