Undefined Index

0

I'm having an error while passing the database data to the view, but the code is pretty much the same as the one I have, and it works fine. The Array with the data is also being sent to the view, but the User in $users['User']['...']; causes this error. How do I resolve this issue?

Controller

    public function admin_mod_password(){

        $this->loadModel('User');
        $users = $this->User->find('all');
        $this->set('users', $users);
        $this->layout='admin_index';

    }

View

<h2>Modificar a Password</h2>
<br>
<table>
<tr>
    <td>
        <p>Nome de Administrador: </p>
    </td>
    <td>
        <?php echo $users['User']['username']; ?>
    </td>
</tr>
<tr>
    <td>
        <p>Password actual: </p>
    </td>
    <td>
        <?php echo $users['User']['password'];?>
    </td>
</tr>

Model

<?php
App::uses('AppModel', 'Model');
//http://book.cakephp.org/2.0/pt/tutorials-and-examples/blog-auth-example/auth.html
class User extends AppModel {   
    public $useTable = 'users';
    public $name = 'User';
    public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A username is required'
            )
        ),
        'password' => array(
            'required' => array(
                    'rule' => array('notEmpty'),
                'message' => 'A password is required'
            )
        ),
        'role' => array(
            'valid' => array(
                'rule' => array('inList', array('admin', 'author')),
                'message' => 'Please enter a valid role',
                'allowEmpty' => false
            )
        )
    );
}
?>

var_dump ($ users)

array (size=1)
  0 => 
    array (size=1)
      'User' => 
        array (size=7)
          'id' => string '1' (length=1)
          'username' => string 'teste' (length=5)
          'password' => string 'teste' (length=5)
          'created' => string '0000-00-00 00:00:00' (length=19)
          'modified' => string '0000-00-00 00:00:00' (length=19)
          'role' => string 'admin' (length=5)
          'user_id' => string '1' (length=1)
    
asked by anonymous 26.03.2014 / 19:00

2 answers

2

Just change the controller to:

    public function admin_mod_password(){

    $this->loadModel('User');
    $users = $this->User->find('first');
    $this->set('users', $users);
    $this->layout='admin_index';

}

Thanks to gmsantos for helping me get there.

    
26.03.2014 / 19:50
2

The error is in your view.

You are returning a list of users in $this->User->find('all'); but in your view you are displaying the data of a single user.

To display the list in the view, use some kind of repeat structure from PHP, such as foreach for example

View

<h2>Modificar a Password</h2>
<br>
<table>
<?php foreach($users as $user): ?>
<tr>
    <td>
        <p>Nome de Administrador: </p>
    </td>
    <td>
        <?php echo $user['User']['username']; ?>
    </td>
</tr>
<tr>
    <td>
        <p>Password actual: </p>
    </td>
    <td>
        <?php echo $user['User']['password'];?>
    </td>
</tr>
<?php endforeach; ?>
</table>
    
26.03.2014 / 19:15