CodeIgniter Forums

Full Version: Loading variables into view loaded by views.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]austintbiggs[/eluser]
So I'm currently loading a library into my controller and passing the variables to a view, but that view loads more views and an error keeps occuring inside those views sayiing that the variables don't exist.

The controller
Code:
$this->data['users'] = $this->ion_auth->get_users_array();
        $this->load->view('body/index', $this->data);

The first view
Code:
<?php $this->load->view('body/includes/sidebar.php', $this->data); ?>

The second view
Code:
<h3><a class="text_shadow" href="#">&lt;?php echo $user['first_name']?&gt;</a></h3>

What am I doing incorrectly? And yes, the library is loading perfectly..

Thanks in advance..

El Forum

[eluser]Shaser[/eluser]
I'm a new one, I think the problem is in this line

$this->data['users'] = $this->ion_auth->get_users_array();

try

$data['users'] = $this->ion_auth->get_users_array();

and load the view like this

&lt;?php $this->load->view('body/includes/sidebar.php', $data); ?&gt;

El Forum

[eluser]oppenheimer[/eluser]
I think the best way is to load the second view in your controller and assign it to a variable. Then just echo that variable in the first view.

In your controller:
Code:
$this->data['users'] = $this->ion_auth->get_users_array();
$this->data['sidebar'] = $this->load->view('body/includes/sidebar.php', $this->data,TRUE);
$this->load->view('body/index', $this->data)

In your first view:
Code:
&lt;?php echo $this->data['sidebar']; ?&gt;

El Forum

[eluser]austintbiggs[/eluser]
Neither of those solutions are working.. I get the same error :[

El Forum

[eluser]tomcode[/eluser]
1. In the controller You load the values into users but later in the second view You call user, unless you iterate anywhere over the users this cannot work


2. Once You have loaded the vars in the controller, they are available in all views You load from then on.
So in the controller :
Code:
$this->data['users'] = $this->ion_auth->get_users_array();

$this->load->view('body/index', $this->data);

in the first view :
Code:
&lt;?php $this->load->view('body/includes/sidebar.php'); ?&gt;

in the second view the $users are available (but NOT $user)


3. I do not know the rest of Your code, but from what You've posted there is no need to declare a class var for Your data, a local var should be sufficient

Code:
$data['users'] = $this->ion_auth->get_users_array();

$this->load->view('body/index', $data);

El Forum

[eluser]Armchair Samurai[/eluser]
Use vars() and variables will be available to all loaded views.:

Code:
$this->load->vars(array(
    'users' => $this->ion_auth->get_users_array()
));
$this->load->view('body/index');

El Forum

[eluser]austintbiggs[/eluser]
While using

In the controller
Code:
public function index()
    {
      $this->data['title'] = "Dashboard";
      $this->data['main_content'] = "auth/login";
      $this->load->vars(array('users' => $this->ion_auth->get_users_array()));
        $this->load->view('body/index', $this->data);

In the first view
Code:
&lt;?php $this->load->view('body/includes/sidebar.php', $this->data); ?&gt;

In the second view
Code:
&lt;?php echo $this->data['users']; ?&gt;

And I'm receiving this error
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: users

Filename: includes/sidebar.php

Line Number: 9

El Forum

[eluser]InsiteFX[/eluser]
It's very simle look at the CodeIgniter User Guide Loader Class!
Code:
$this->load-vars($data);
$this->load->view('your_view');

InsiteFX

El Forum

[eluser]Armchair Samurai[/eluser]
I suggest taking a look at the [url="http://ellislab.com/codeigniter/user-guide/libraries/loader.html"]User Guide[/url]. Try putting all your variables in one array:

Code:
$this->load->vars(array(
    'users' => $this->ion_auth->get_users_array(),
    'title' => 'Dashboard',
    'main_content' => 'auth/login'
));
$this->load->view('body/index');

El Forum

[eluser]austintbiggs[/eluser]
Problem solved! I was being horribly idiotic and using the wrong function and calling an array.
Pages: 1 2