• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change the main content per controller change

#1
I've been making school projects and I need ideas on how it can be done
I have 2 view files that is always included when making controller methods

I create a function where the main content will load based on the parameter supplied

Here's the code:
Code:
public function nextPage($link){
 $this->load->view('inc/header_sidebar');
 $this->load->view('pages/' . $link);
 $this->load->view('inc/footer_sidebar');
}

Then using the method like this:
Code:
public function user_index(){

$this->nextPage('user_view');
}

public function patient_index(){
$this->nextPage('patient_index');
}

I've been thinking that it is not a good approach since I think the header and footer will also load upon loading the said section
Reply

#2
Make a MY_Controller (in application/core).
Create a method in it that renders the pages, the way you want.

PHP Code:
class MY_Controller extends CI_Controller {

    public 
$data = array();

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

    protected function 
render_page($view)
    {
        
$this->load->view('inc/header_sidebar');
        
$this->load->view($view,$this->data);
        
$this->load->view('inc/footer_sidebar');
    }    



Let all your controllers extend this MY_Controller.
If you want to load a certain page with the default header and footer, just use $this->render_page('viewfile-name');
All the data that you want to pass to the view, can be added to the $this->data array, like:
PHP Code:
$this->data['records'] = $this->sample_model->get_all(); 
Reply

#3
This is how I do it:


PHP Code:
public function foo()
{
 
   // Passing along vars to the view when necessary
 
   $view_data NULL;

 
   $data = [
 
       // $content is in the main template
 
       'content' => $this->load->view('foo/bar'$view_dataTRUE )
 
   ];

 
   // Main template has the complete page (header, footer, everything) in it
 
   $this->load->view('templates/main'$data);

Reply

#4
I forgot to mention that the method is in MY_Controller.
I didn't try yet those ways you provide guys but would it accomplish what I want to achieve.

Like this, assumed I have a dashboard and when the index page is loaded all resources like jquery would also be loaded. Then when navigating to links inside the dashboard only the content wrapper that will change and it will not load the resources again.
Reply

#5
Quote:assumed I have a dashboard and when the index page is loaded all resources like jquery would also be loaded. Then when navigating to links inside the dashboard only the content wrapper that will change and it will not load the resources again.

Nice thought, but it would mean that all content within the dashboard is shown via one URL. Once you change the URL (e.g. because you switch to another method inside the controller), a new page is loaded.
If you just want to change some elements on your page without refreshing the whole page, you will need AJAX.
Reply

#6
Okay, I'll try it. Could you show me some guide how to load the content
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.