EllisLab text mark
Advanced Search
     
Passing data to view not working with multiple views
Posted: 25 April 2012 10:37 AM
Joined: 2011-10-07
7 posts

If I pass data to my view like this, it works just fine:

$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:

$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:

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

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

 
Posted: 25 April 2012 01:40 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6596 posts
$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.

$data['errors'= array('foo' => 'bar');
<?php echo $errors['foo'];> 

 

 Signature 

Certified State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

Skype: insitfx

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 25 April 2012 02:22 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-10-07
7 posts
InsiteFX - 25 April 2012 01:40 PM
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'];> 

But that’s exactly what I am doing.

 
Posted: 25 April 2012 02:36 PM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6596 posts

Try:

$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.

 

 Signature 

Certified State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

Skype: insitfx

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 26 April 2012 01:06 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

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.

 
Posted: 26 April 2012 05:54 AM   [ # 5 ]   [ Rating: 0 ]
Joined: 2007-11-23
332 posts

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