in the sections_controller i use loadmodel to print username in sections/index file but i get error :
Notice (8): Undefined variable: user [APP\views\sections\index.ctp, line 3]
public function index ()
{
$this->loadModel('User');
$user = $this->User->find('all');
$this->Section->find('threaded', array('order' => array('Section.created ASC')));
$this->set('data','user');
}
in the sections/index
<div><?php echo $user['User']['username']; ?></div>
You’re not assigning the variable properly, try this instead:
$userpointing to your find result and set asuser, since that’s what you’re trying to call in your view. Or alternatively use thecompactnotation to achieve the same:What you are doing now is setting a variable called
datawith the string valueuser, so when you would do this in your view:It would return
useras a string in your view.EDIT
Since a find(‘all’) will return an array, make sure you loop over the results in your view as well, like this:
Or if you want a specific result, call it’s array key, which for the first user would be: