0

I am using codeigniter framework and on one of the controllers I have tis code below

public function index() {
    $this->data['users'] = $this->user_m->get();
    $this->data['subview'] = 'admin/usuarios/index';
    $this->load->view('admin/layout_principal', $this->data);
}

My problem is when I try to use the data users and subviews into a view. As you can see "subviews" has a location of another view file, and when I call "subviews" in the main view like this:

<div class="span9">
<?php $this->load->view($subview); ?>
</div>

I get this error:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: subview
Filename: admin/layout_principal.php
Line Number: 19
An Error Was Encountered
Unable to load the requested file: .php*

And I said "well, its just a location I'll put it directly, and go on loading my users data, but then when I use "users" from the data like this:

<?php if(count($users)): foreach($users as $user):?>        
<tr>
<td><?php echo anchor('admin/usuario/editar/' . $user->id, $user->email);?></td>
<td><?php echo btn_edit('admin/usuario/editar', $user->id); ?></td>
<td><?php echo btn_delete('admin/usuario/borrar', $user->id); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">No users found</td>
</tr>
<?php endif;?>

I get this error:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: users
Filename: usuario/index.php
Line Number: 15

The view it's not recognizing the data variables, I had searched and tried for ways to solve this but I still with the same problem, and well, I posted this for help as last resource, can you help me? what can I do to make the view recognize the data, because I can't find what's wrong with this.

2
  • You are not getting value within your variable users try to check if isset or not Commented Apr 16, 2015 at 6:43
  • Have you loaded your model user_m i.e $this->load->model('user_m'); in index I don't see it. Commented Apr 16, 2015 at 6:44

3 Answers 3

0

I can not see that you have loaded the user model

$this->load->model('user_m');

Controller

public function index() {
    $this->load->model('user_m');

    $this->data['users'] = $this->user_m->get();
    $this->data['subview'] = 'admin/usuarios/index';
    $this->load->view('admin/layout_principal', $this->data);
}

Also you need to load view file sub view like so

public function index() {
    $this->load->helper('url'); // Should autoload it.
    $this->load->model('user_m');

    $data['users'] = $this->user_m->get();
    $data['subview'] = $this->load->view('sub_view');

    //if any data in sub view
    //$data['subview'] = $this->load->view('sub_view', $data);

    $this->load->view('admin/layout_principal', $data);
}

If you need to load controller into controller you need HMVC

I also have not heard of <?php echo btn_delete();?> and <?php echo btn_edit();?> http://www.codeigniter.com/userguide2/helpers/url_helper.html

View Try

<?php if(count($users)): foreach($users as $user):?>        
<tr>
<td><?php echo anchor('admin/usuario/editar' .'/'. $user['id']); ?></td>
<td><?php echo anchor('admin/usuario/borrar' .'/'. $user['id']); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">No users found</td>
</tr>
<?php endif;?>
Sign up to request clarification or add additional context in comments.

3 Comments

I load the model in another controller, so the "user" controller extends the controller where I load the model, but I put it in the "user" controller just in case. I change "$this->" by just "$data" like you told me, and sent the variable, but still the same problem, the view is not getting the data, it says that the variable "subview" and "users" inside the data is undefined.
I also forgot to mention that the location and name of the view in the controller are correct.
Sorry, I was having a logical error while putting your solution, I finally could load the data :) thanks a lot man!
0

in your controller function

public function index() {
    $this->data['users'] = $this->user_m->get();
    $this->data['subview'] = 'admin/usuarios/index';
    $this->load->view('admin/layout_principal', $this->data);
}

in your view file:

<div class="span9">
<?php 
 if(isset($subview))
foreach($subview as $subvi)
{
  echo $subvi;
}
 ?>
</div>

for users data in your file as given below:

<?php 
if(count($users)!=0) (OR) if(isset($users))
foreach($users as $user):?>        
<tr>
<td><?php echo anchor('admin/usuario/editar/'.$user->id, $user->email);?>    </td>
<td><?php echo btn_edit('admin/usuario/editar', $user->id); ?></td>
<td><?php echo btn_delete('admin/usuario/borrar', $user->id); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">No users found</td>
</tr>
<?php endif;?>

1 Comment

I change the variables calling on the views like you told me, but the problem is the same, as the view is not getting the data variables, it doesn't show nothing, the issue is that the variables are like if they doesn't exist even though I'm using them in the controller and sending them to the view.
0

Try to use below code in controller(i think you must have loaded the "user_m" model in function __construct(){} ) :

public function index() {
    $data = array();
    $data['users'] = $this->user_m->get();

    $data['subview'] = $this->load->view('admin/usuarios/index', $data, true);
    $this->load->view('admin/layout_principal', $data);
}

And then in view('admin/layout_principal'):

<div class="span9">
<?php echo $subview; ?>
</div>


<?php if(count($users)): foreach($users as $user):?>        
<tr>
<td><?php echo anchor('admin/usuario/editar/' . $user->id, $user->email);?></td>
<td><?php echo btn_edit('admin/usuario/editar', $user->id); ?></td>
<td><?php echo btn_delete('admin/usuario/borrar', $user->id); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">No users found</td>
</tr>
<?php endif;?>

But try to do all looping and view loading in controller itself... I can explain how ifyou want me to... because view is only for html and php variables.. it should not contain code for loop and load view... but this must part of controller code.. and load in a variable and just echo the variable in the view... must take care of this.. for maintain standard MVC ...

1 Comment

Even though if I do all in the controller, I think the view would still without recognizing the data variables, the problem is not the looping or anything like that, the problem is that the view is not even loading the data for the loop or for the echo of the subview, otherwise I would see another type of error and not an "undefined variable" error, and I don't see why is not loading the data.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.