0

I am developing an application in codeigniter containing different modules e.g buyer, seller, public. All these modules use same header file. I want a global variable declared in buyer, seller and public controller so that when I load views of one module this variable should help customizing parts of header for seller depending upon its value. Same goes for buyer and public modules.

I want to ask what is the way to have a variable declared at controller scope and then use its value in its views. I have tried declaring and assigning its value in constructor but I get undefined variable error when loading the views. I also tried this way:

class Seller extends CI_Controller {

public $pagetype="seller";
public function __construct()
    {
        parent::__construct();
    }

But I still get undefined variable error. I can pass value of page type when loading view but I have to do that for each view, which is bad and cumbersome way because I have around 25-30 views in each controller.

Any help?

Hammad
  • 2,097
  • 3
  • 26
  • 45

3 Answers3

0

Consider making your own class - look into Singleton or Registry design patterns... but also read this: What is so bad about singletons?

Community
  • 1
  • 1
Ryan
  • 5,959
  • 2
  • 25
  • 24
0

in your controller you can make data globally available in the view layer using this

$this->load->vars($data);

where $data is an array of key => values. you could put this in your constructor, or parent constructor

David Chan
  • 7,347
  • 1
  • 28
  • 49
0

you could assign the main CodeIgniter object to a variable and then use load->get_var($key) to get the view variable.

function something() {
    $ci =& get_instance();
    $myvar = $ci->load->get_var(‘myvar);
}
caacuna
  • 11
  • 1