i have a controller page like this:
class UsersController extends AppController {
var $helpers = array ('Html','Form');
var $name = 'Users';
function index() {
$this->set('view_users', $this->User->find('all'));
}
}
then i have another controller page like this:
class PostsController extends AppController {
var $helpers = array ('Html','Form');
var $name = 'Posts';
function index() {
$this->set('edit_users', $this->Post->find('all'));
}
}
then i have the index file:
<?php foreach($view_users as $value): ?>
<p>id - <?php echo $value['User']['first']; ?></p>
<?php endforeach; ?>
<?php foreach($edit_users as $value): ?>
<p>id - <?php echo $value['Post']['first']; ?></p>
<?php endforeach; ?>
the problem i get is :Notice (8): Undefined variable: view_users [APP\views\posts\index.ctp, line 6]
very strange since the edit_users var works.
What can be the problem?
thanks
The
UsersControllerand thePostsControllerare different controllers. They use different views and even though they both have anindexaction, they are called separately and use different views.When you go to
/users/index, it calls theindexaction of theUsersControllerwhich sets the$view_usersvariable.When you go to
/posts/index, it calls in theindexaction of thePostsControllerwhich sets the$edit_users.The problem: You
PostsControllerloginaction isn’t setting a value for$view_usersso the variable is undefined. Assuming that youPostmodel has a relationship with theUsermodel, you should be able to add this to thePostsControllerindexaction to solve your problem:Alternatively, you could change the view to use
emptyto check if the variable was set first: