CodeIgniter Forums
Passing data to view not working with multiple views - 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: Passing data to view not working with multiple views (/showthread.php?tid=51226)



Passing data to view not working with multiple views - El Forum - 04-25-2012

[eluser]xbonez[/eluser]
If I pass data to my view like this, it works just fine:

Code:
$data['errors'] = array(
    'foo' => 'bar'
   );
$this->load->view('register',$data);

However, if I call another view before calling the 'register' view, I cannot access the data in the view:

Code:
$data['errors'] = array(
    'foo' => 'bar'
   );
$this->load->view('fragments/header',array('title' => 'Register a New Accout'));  
$this->load->view('register',$data);

In both cases, my register.php view looks like this:

Code:
<?php echo $errors['foo']; ?>

Am I missing something, doing something wrong, or is this a bug?


Passing data to view not working with multiple views - El Forum - 04-25-2012

[eluser]InsiteFX[/eluser]
Code:
$data['errors'] = 'value';

<?php echo $errors['foo']; ?>

// should be
<?php echo $errors; ?>

If you want to do it your way then you will need to access it different.
Code:
$data['errors'] = array('foo' => 'bar');
<?php echo $errors['foo'];>



Passing data to view not working with multiple views - El Forum - 04-25-2012

[eluser]xbonez[/eluser]
[quote author="InsiteFX" date="1335375659"]
Code:
If you want to do it your way then you will need to access it different.
[code]
$data['errors'] = array('foo' => 'bar');
<?php echo $errors['foo'];>
[/quote]

But that's exactly what I am doing.


Passing data to view not working with multiple views - El Forum - 04-25-2012

[eluser]InsiteFX[/eluser]
Try:
Code:
$data['errors'] = array(
    'foo' => 'bar'
);

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

$this->load->view('fragments/header', array('title' => 'Register a New Accout'));  
$this->load->view('register');

It could be that the array in your first view is over writing the $data not sure and no way to test here.



Passing data to view not working with multiple views - El Forum - 04-25-2012

[eluser]Aken[/eluser]
There should be no reason your code can't function as you've put it. You probably have some other code that is not working the way you're expecting.


Passing data to view not working with multiple views - El Forum - 04-26-2012

[eluser]gRoberts[/eluser]
Since the dawn of CodeIgniter, I've only ever used $this->load->view once within an Action.

Once I have loaded a single view, within that view, I then use includes to include header, footer, nav and any additional views that are needed.

This means that the initial data element passed to $this->load->view('view', $data) is accessible across all of the views being displayed.

Each to their own of course, but it might be a solution to your problem also.

HTH