cakephp - cake php, undefined variable problem -


i have controller page this:

class userscontroller extends appcontroller { var $helpers = array ('html','form'); var $name = 'users';  function index() {     $this->set('view_users', $this->user->find('all')); } } 

then have controller page this:

    class postscontroller extends appcontroller { var $helpers = array ('html','form'); var $name = 'posts';  function index() {     $this->set('edit_users', $this->post->find('all')); } } 

then have index file:

<?php foreach($view_users $value): ?> <p>id - <?php echo $value['user']['first']; ?></p> <?php endforeach; ?>  <?php foreach($edit_users $value): ?> <p>id - <?php echo $value['post']['first']; ?></p> <?php endforeach; ?> 

the problem :notice (8): undefined variable: view_users [app\views\posts\index.ctp, line 6]

very strange since edit_users var works.

what can problem? thanks

the userscontroller , postscontroller different controllers. use different views , though both have index action, called separately , use different views.

when go /users/index, calls index action of userscontroller sets $view_users variable.

when go /posts/index, calls in index action of postscontroller sets $edit_users.

the problem: postscontroller login action isn't setting value $view_users variable undefined. assuming post model has relationship user model, should able add postscontroller index action solve problem:

$this->set('view_users', $this->post->user->find('all')); 

alternatively, change view use empty check if variable set first:

if ( ! empty($view_users) )   foreach ( $view_users view_user )   {     // useful.   } 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -