Pawan Choudhary
Pawan Choudhary

Reputation: 1073

array of data variables passed to a view , is passed to all views, is this a bug in codeigniter or i'm doing wrong?

if i load 2 views like this in codeigniter in the same model :

$first = array('select' => 'anything');
$first = $this->load->view('first', $first, TRUE);
echo $first;
$second = array();
$second = $this->load->view('second', $second, TRUE);
echo $second;

Then the array of data ($first) passed to the first view is also available to the second view too. like if i

echo $select;
variable in second view the value is echoed successfully which is the same as echoed in the first view, how can it be possible if i'm not passing any data to the second view? why it's getting the data passed to first view? i'm using codeigniter 2.0, which is the latest version of codeigniter i think.

Thanks in advance.

Upvotes: 1

Views: 111

Answers (1)

jondavidjohn
jondavidjohn

Reputation: 62392

I believe it is because it is buffering the output and the view data is available globally to loaded views until the request is completed.

Which is actually AFTER the second view is output.

Have you tested this w/o using echo like this?

$first = array('select' => 'anything');
$this->load->view('first', $first);

$second = array();
$this->load->view('second', $second);

Do you get the same behavior?

Not sure if it is a matter of incorrect language you're using, but you should be loading these views in the Controller, not the Model in keeping with the model the framework was built on, MVC

UPDATED TO ADD LINK ABOUT LOADING VIEWS SEQUENCIALLY

http://www.askaboutphp.com/48/codeigniter-organizing-views-simply.html

Upvotes: 1

Related Questions