2

I am a newbie with codeigniter. I am trying to write an application using mysql database. In my site I want to use menu as :

+Homepage
+About
+Services
  +Education services
  +neurofeedback
  +biofeedback

I need some information to understand. I use pages controller as main pages controller:

<?php 

class Pages extends CI_Controller {

        public function view($page = 'home')
        {$this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);

my questions are :

1) where the menu controller must be coded inside pages controller or seperate one?

2) how can i make the menu controller from database ?

3) How can i make relation with the menu id and the page id?

I made lots of research but i need a little bit more understanding .

Thank you for your help.

Edit : I have used MY_Controller as you say .

This is my pages controller :

class Home extends MY_Controller {
         function __construct() {
    parent::__construct();
  }

        public function view($page = 'home')
        {
         $this->load->helper('text');
            $data['records']= $this->services_model->getAll();
            if ( ! file_exists('application/views/pages/'.$page.'.php'))
            {
                // Whoops, we don't have a page for that!
                show_404();
            }

            $data['title'] = ucfirst($page); // Capitalize the first letter


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


        }

}

1 Answer 1

5

where the menu controller must be coded inside pages controller or seperate one?

Assuming that you have a template that must be followed by all pages, I suggest you to do this.

1. Create a basic controller

In the ./application/core/ folder, create a file called MY_Controller

class MY_Controller extends CI_Controller {

  protected $data = array();

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

  function render_page($view) {
    //do this to don't repeat in all controllers...
    $this->load->view('templates/header', $this->data);
    //menu_data must contain the structure of the menu...
    //you can populate it from database or helper
    $this->load->view('templates/menu', $menu_data);
    $this->load->view($view, $this->data);
    $this->load->view('templates/footer', $this->data);
  }

}

2. Create one controller to each page and use MY_Controller instead of CI_Controller

class Homepage extends MY_Controller {
  function __construct() {
    parent::__construct();
  }

  function index() {
    //define data that the view can access
    $this->data['someDataToView'] = 'Some data';
    $this->render_page('pages/homepage');
  }

}

how can i make the menu controller from database?

Well, you will not have a controller for the menu, but a view instead.

Possibilities to the menu

  1. Create a view for the menu, load the records from the database in MY_Controller, load the view in render_page();
  2. Create a view for the menu, create a Helper function that defines the menu structure and use in MY_Controller, load the view in render_page();

Example of menu template (adjust for your scenario):

./application/views/templates/menu.php

<ul>
<?php foreach($menus as $menu): ?>
  <li><a href='<?php print $menu["url"] ?>'><?php print $menu["title"] ?></a></li>
<?php endforeach; ?>
</ul>

edit

Given your Home controller, I think the error is in your file_exists check. See the Home controller that I change:

class Home extends MY_Controller {
  function __construct() {
    parent::__construct();
  }

   public function view($page = 'home') {
     $this->load->helper('text');
     //always use $this->data
     $this->data['records']= $this->services_model->getAll();
     if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
     {
       //check the content of APPPATH.'views/pages/'.$page.'.php'
       // Whoops, we don't have a page for that!
       show_404();
     }

     $this->data['title'] = ucfirst($page); // Capitalize the first letter

     //if you use the MY_Controller, check the render_page function...
     //$this->load->view('pages/'.$page, $data);
     $this->render_page('pages/'.$page)
   }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Sergio, am i right that I have to build a controller in "application/core/"? after that I have to build a pages controller in "application/controller"?
The controller in /application/core means that you will extend the CI_Controller, adding more functionalities. Note that you don't have a url for MY_Controller, but the others controllers in /application/controller will extend MY_Controller. You can see more info here
I get 404 error now. can You check my controllers ? I put them in the editted question.
Sergio , I have to ask you a question about $this->data .. I changed all my application to work with ajax. I can see the home.php but about.php does not let me to use database. Can I ask you your ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.