Quantcast
Channel: CodeIgniter Forums - All Forums
Viewing all articles
Browse latest Browse all 14115

Make the program more correct

$
0
0
Good day!
This is a simple view of my program. I have many controllers that look something like this

PHP Code:
class Home extends CI_Controller {
    private 
$data;
    public function 
__construct()
    {
        
parent::__construct();

        
$this->load->model('home_model');

        
$this->data['header'] = $this->home_model->GetDataHeader();
        
$this->data['footer'] = $this->home_model->GetDataFooter();
        
$this->data['body'] = $this->home_model->GetDataBody();
    }
    public function 
page1(){
        
$this->data['page'] = $this->home_model->GetDataPage(1);

        
$this->load->view('header',$this->data['header']);
        
$this->load->view('body',$this->data['body']);
        
$this->load->view('page1'$this->data['page']);
        
$this->load->view('footer',$this->data['footer']);
    }
    public function 
page2(){
        
$this->data['page'] = $this->home_model->GetDataPage(2);

        
$this->load->view('header',$this->data['header']);
        
$this->load->view('body',$this->data['body']);
        
$this->load->view('page2'$this->data['page']);
        
$this->load->view('footer',$this->data['footer']);
    }
    public function 
page3(){
        
$this->data['page'] = $this->home_model->GetDataPage(3);

        
$this->load->view('header',$this->data['header']);
        
$this->load->view('body',$this->data['body']);
        
$this->load->view('page3'$this->data['page']);
        
$this->load->view('footer',$this->data['footer']);
    }


I think this is not very good. And change to this
PHP Code:
class MY_Loader extends CI_Loader{
    public function 
__construct(){
        
parent::__construct();
    }

    public function 
my_load($name$vars = array())
    {
        
$this->view('header',$vars['header']);
        
$this->view('body',$vars['body']);
        
$this->view($name,$vars['page']);
        
$this->view('footer',$vars['footer']);
    }
}

class 
Home extends CI_Controller {
    private 
$data;
    public function 
__construct()
    {
        
parent::__construct();

        
$this->load->model('home_model');

        
$this->data['header'] = $this->home_model->GetDataHeader();
        
$this->data['footer'] = $this->home_model->GetDataFooter();
        
$this->data['body'] = $this->home_model->GetDataBody();
    }
    public function 
page1(){
        
$this->data['page'] = $this->home_model->GetDataPage(1);

        
$this->load->my_load('page1',$this->data);
    }
    public function 
page2(){
        
$this->data['page'] = $this->home_model->GetDataPage(2);

        
$this->load->my_load('page2',$this->data);
    }
    public function 
page3(){
        
$this->data['page'] = $this->home_model->GetDataPage(3);

        
$this->load->my_load('page3',$this->data);
    }


Now, if i change thing related with "load view", i will do it in one place, not in all functions of all controllers.

But if i change the data for the header or for the footer i will change each controller/ And i think this is not good.
How can I change the program to fix this situation?

Thank you

Viewing all articles
Browse latest Browse all 14115

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>