CodeIgniter Forums
How to make it available in every function? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to make it available in every function? (/showthread.php?tid=49774)



How to make it available in every function? - El Forum - 03-02-2012

[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);
  }

}



How to make it available in every function? - El Forum - 03-03-2012

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


How to make it available in every function? - El Forum - 03-03-2012

[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);
  }

}



How to make it available in every function? - El Forum - 03-03-2012

[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);
  }
}




How to make it available in every function? - El Forum - 03-04-2012

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