Welcome Guest, Not a member yet? Register   Sign In
Passing data to view not working with multiple views
#1

[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?
#2

[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'];>
#3

[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.
#4

[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.
#5

[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.
#6

[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




Theme © iAndrew 2016 - Forum software by © MyBB