Reputation: 27038
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
Upvotes: 2
Views: 16164
Reputation: 24969
The UsersController
and the PostsController
are different controllers. They use different views and even though they both have an index
action, they are called separately and use different views.
When you go to /users/index
, it calls the index
action of the UsersController
which sets the $view_users
variable.
When you go to /posts/index
, it calls in the index
action of the PostsController
which sets the $edit_users
.
The problem: You PostsController
login
action isn't setting a value for $view_users
so the variable is undefined. Assuming that you Post
model has a relationship with the User
model, you should be able to add this to the PostsController
index
action to solve your problem:
$this->set('view_users', $this->Post->User->find('all'));
Alternatively, you could change the view to use empty
to check if the variable was set first:
if ( ! empty($view_users) )
foreach ( $view_users as view_user )
{
// Do something useful.
}
Upvotes: 3