CodeIgniter Forums
pls help - 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: pls help (/showthread.php?tid=57131)



pls help - El Forum - 02-19-2013

[eluser]newbie110[/eluser]
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.


pls help - El Forum - 02-19-2013

[eluser]Otemu[/eluser]
Quite a few ways to do this:

If it just one controller then just put your variables at the top something like this:
Code:
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