0

i have a codeigniter application with 5 controllers one is the 'base' and the rest are inheriting form it, i am using global variable in the base to load in the view but it's not working i get (Undefined variable) when i load the view how can I fix this?

i'm using this function in the base to load the views


function _setContent($tplFile) {
            ob_start();
            $this->load->view($this->theme_dir . '/' . $tplFile, $this->tplData);
            $_content = ob_get_contents();
            ob_end_clean();

            $this->tplData['_content'] = $_content;
            $this->load->view($this->theme_dir . '/default', $this->tplData);
        }

$this->tplData // is the global variable
3
  • 2
    Not answering your question but you can set true as the third parameter to the view() function to return the content rather than echo-ing it (so you dont need to use ob_* functions) Commented Apr 25, 2012 at 11:22
  • 1
    Mamoum, what's the exact message you get? It's strange you get the Undefined error for $this->tplData, are you sure the error doesn't come from the view, where you call array indexes you didn't pass have? Commented Apr 25, 2012 at 11:28
  • the error is it thhe views when I call the array indexes it says : Undefined variable ... Commented Apr 25, 2012 at 22:55

2 Answers 2

1

you can use config class.

$this->config->set_item('global_variable', $my_var);

now you can use this variable wherever you want

$this->config->item('global_variable');
Sign up to request clarification or add additional context in comments.

Comments

0

try using define() instead of global to set your variable - http://php.net/manual/en/function.define.php

13 Comments

logically speaking, define is for constants and in OP's logic that ISN'T a constant. Although both available gloablly, globals and constats have different purposes, they're not interchangable
Well, what is the type of "tplData" ?
Honestly, I don't find it clear, but since it's related to views I imagine it's something that changes from every method, so that one view loads something, another loads some other set of data and so on. If you define it as a constat, you need to redefine it everytime you want to pass the view different datas.
I agree with @DamienPirsy. Define doesn't seem like the right approach for this.
Looking at the code, $this->tplData should hold all the variables passed to the view. The more I think about it, the more making it a constant gets wrong. And the more I don't see the need of making it a global variable too.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.