Welcome Guest, Not a member yet? Register   Sign In
How to make it available in every function?
#1

[eluser]CocoMansur[/eluser]
Hi,

i have a model that retrieves data from a database and was returned to a variable in the controller then will be displayed in view.

my problem is, in every function within the controller i always have to call that model. is there a way that will do this in default without calling the model on each of the functions within the controller?

here is a sample code:

Code:
class Base extends CI_Controller{

  public function welcome(){
   $data['get_latest'] = $this->log_templates->get_latest_log();
   (other codes here...)
   $this->load->view('template',$data);
  }
  
  public function marketting(){
   $data['get_latest'] = $this->log_templates->get_latest_log();
   (other codes here...)
   $this->load->view('template',$data);
  }

  public function documentation(){
   $data['get_latest'] = $this->log_templates->get_latest_log();
   (other codes here...)
   $this->load->view('template',$data);
  }

}
#2

[eluser]solid9[/eluser]
try putting the code in __construct()
#3

[eluser]CocoMansur[/eluser]
I tried that already but it doesn't work.

when i load my default page it displays, undefined variable get_latest.

here is the code with the construct

Code:
class Base extends CI_Controller{

  public function __construct(){
  parent:: __construct();
   $this->do_this();
}
  public function do_this(){
    $data['get_latest'] = $this->log_templates->get_latest_log(); // gets the latest log
}

  public function welcome(){  
   (other codes here...)
   $this->load->view('template',$data);
  }
  
  public function marketting(){
   $data['get_latest'] = $this->log_templates->get_latest_log();
   (other codes here...)
   $this->load->view('template',$data);
  }

  public function documentation(){
   $data['get_latest'] = $this->log_templates->get_latest_log();
   (other codes here...)
   $this->load->view('template',$data);
  }

}
#4

[eluser]solid9[/eluser]
try this,

Code:
class Base extends CI_Controller{
public $data = array();

  public function __construct(){
  parent:: __construct();
   $this->data = $this->log_templates->get_latest_log();
}

  public function welcome(){  
   (other codes here...)
   $this->load->view('template',$this->data);
  }
  
  public function marketting(){
   (other codes here...)
   $this->load->view('template',$this->data);
  }

  public function documentation(){
   (other codes here...)
   $this->load->view('template',$this->data);
  }
}

#5

[eluser]CocoMansur[/eluser]
Thanks solid9. it worked perfectly. now, i just have to modify some of the codes.




Theme © iAndrew 2016 - Forum software by © MyBB