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 8174173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:23:01+00:00 2026-06-06T22:23:01+00:00

I have a menu on my page that is loaded on every page. The

  • 0

I have a menu on my page that is loaded on every page. The menu data is fetched from the database through a model. My first thought was to call the model from the controller, and then pass it to the view on every page. But that results in “messy” code, if i ever forget to pass the menu data to the view, it will result in an ugly error (or just no menu items).

So i came up with the solution of fetching the menu items through a helper, and then just call the helper function from the view. It makes more sense, because i only have the code in one place (the menu view).

My views are set up in this way: Controller calls “page” view which then loads the header view, menu view, the appropriate content view, and lastly the footer view. The helper is only called from one place, the menu view.

Normally you can’t even load models from helpers, but i did a workaround using $i = get_instance(); and then loading the model through that instance; $i->load->model().

I have a feeling this is not the way to go, but are there any better ways?

Edit: To put it in a better way:

I want:
view -> get data -> display

not:
controller -> get data -> pass to view -> display

I’m just not sure if that’s “Ok” to do, since it disregards the MVC model completely.

  • 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-06T22:23:03+00:00Added an answer on June 6, 2026 at 10:23 pm

    I think that the solution is easier than you think.

    If now you’re doing something like this in your helper:

    create_menu()
    {
        $menu_items = $this->db->query('')->result();
        // creating the menu here
    }
    

    You could just change the function to accept input like this and still follow the MVC pattern.

    Helper

    create_menu($input)
    {
        $menu_items = $input;
        // creating the menu here
    }
    

    Model:

    get_menu_data()
    {
       $menu_items = $this->db->query('')->result();
    }
    

    Does this make sense?

    Edit:

    This is the way I did it on one of the projects:

    I extended my standard controller. Within the constructor of that controller I called the model and grabbed the data:

    $this->menu_items = $this->some_model->get_menu_items();
    

    Within a view nav.php:

    if(!empty($this->subnav_item))
    {
       // Generate menu
    }
    

    This way the MVC is intact, but I dont have to worry about passing variables.

    EDIT 2

    How to extend the standard controller:

    Create a file MY_Controller.php in application/core

    class MY_Controller extends CI_Controller {
    
        public $menu_items = '';
    
        function __construct()
        {
            parent::__construct();
            $this->load->model('some_model_that_you_always_use');
            $this->load->library('some_library_that_you_always_use');
            $this->menu_items = $this->some_model->get_menu_items();
        }
    
    }
    

    When you create a new controller, you extend MY_Controller instead of CI_Controller like this:

     class Something extends MY_Controller {
    
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So on my page I have a menu that is fixed to the top
In my web page, I have a menu ( HorizontalPanel ) that should be
I have a drop down menu that looks great when the page it scrolled
Scenario: Every page on the website will have a menu bar at the top
I have an ASP.NET MVC Page that has Left Menu navigation that is built
I have my Zend_Navigation loaded from a PHP array (but that's irrelevant...) and I'm
I have a page that has a left menu in it. This left menu
I have a YUI (2.8.x) MenuButton that is lazily loaded via an AJAX call.
I have a page that has a menu and a content area. I dynamically
I have an unordered list that is being loaded from an external file using

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.