Undefined Index

Asked

Viewed 129 times

0

I’m having an error passing the data from the database to the view, only the code is pretty much the same as the one I have, and it works correctly. The Array with the data is also being sent to the view, but the User in $users['User']['...']; causes this error. How to solve this problem?

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)
  • Post the result of var_dump() of the variable $users

  • @gmsantos I’ve already put.

2 answers

2

The error is in your view.

You are returning a list of users on $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 repeating structure of 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>
  • I had already experienced this, however I will try another find().

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.