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

  • Home
  • SEARCH
  • 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 1075143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:13:58+00:00 2026-05-16T21:13:58+00:00

I generate a menu from a database table using a function, and I’ve placed

  • 0

I generate a menu from a database table using a function, and I’ve placed this in an extended base controller class:

<?php

class MY_Controller extends Controller {

    public function __construct()
    {
        parent::Controller();
    }

    public function category_menu()
    {
        $this->load->model('category_model', 'category');
        $categories = $this->category->get_categories();

        $menu ="<ul class=\"menu_body\" id=\"nav_categories\">\n";
        foreach($categories->result() as $row)
        {
            $menu .= "\t<li>" . anchor('listing/view' . $row->url, $row->name) . "</li>\n";
        }
        $menu .= "</ul>\n";

        return $menu;

    }
}

then naturally my controller looks like ~

<?php

class Site extends MY_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $data['menu'] = $this->category_menu();
        $this->load->view('view', $data);
    }
}

this does work, but it seems inefficient to have to do this for ~every~ page/view?

Or is this just a limitation of CI/MVC and there’s no other way of doing it.

thanks for any insight

  • 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-05-16T21:13:59+00:00Added an answer on May 16, 2026 at 9:13 pm

    The better way of doing this is rendering content in views. You can have partial templates, you don’t need to do string appending in the controller:

    $categories = $this->category->get_categories();
    $data['menu'] = $this->load->view('menu', array('data'=>$categories), TRUE);
    $this->load->view('view', $data);
    

    The TRUE in the call to view tell the function to return the rendered content and not put it into the buffer. You can then pass it to ‘view’. You can also get the categories and pass them into the view ‘view’ and load the partial template from there.

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

Sidebar

Related Questions

I have a database that looks like this: I can generate a tree from
To generate the following markup: <label class=foo>Bar</label> The PHP will look something like: <?php
I have a recursive function being used to generate a menu on my site.
I want to use ADO via ODBC to pull records from a database table
I want to generate the drop down menu, which the menu are generate from
I have a site Index.html and there is a categories menu generated from database.
I have configured spring security core plugin using requestmap table inside the database.. Now
i have a menu generated dynamically by taking title from database. i have set
I generate ListBox menu from XML. I use datatemplate to style the behavior of
I have a database like this: table pepak, table category, table subcategory I just

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.