CodeIgniter Forums

Full Version: pls help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

I am having a couple of common variables which are being shown on each view page, for that I have to load these variables in each function of the controller. Is there any way to globally load them so that I don't have to copy the same code again and again.

El Forum

Quite a few ways to do this:

If it just one controller then just put your variables at the top something like this:
class MyGlobalVariables extends CI_Controller{

$myVariable = 'hi am a global variable';

  function someCoolFunction(){
     echo $this->myVariable ;
  function someOtherCoolFunction(){
     echo $this->myVariable ;
Create your variables within My_Controller have it extend CI_Controller, then instead of using class myGlobalVariables extends CI_Controller you would have myGlobalVariables extends My_Controller.
Create a library
Simple method would be just to save your variables in the config
You could use constants \application\config\constants.php