I would like to resolve the Invalid value error in foreach.

Asked 2 years ago, Updated 2 years ago, 47 views

CakePHP has created a function to display username from the database using the search function.

Notice(8): Undefined variable: users [APP/View/User/find.ctp, line 6]
Warning(2): Invalid argument supplied for foreach() [APP/View/User/find.ctp, line 6]

The error stopped.
I think this is quite a rudimentary error, but I would appreciate it if you could tell me how to solve it.

<div class="row-fluid">
<div class="span9">

   <table class="table">

   <?php foreach($users as$user): ?>///// error point
        <tr>
            <td><?phpechoh($user['users']['id']);?>/td>
            <td><?phpecho$this->Html->link($user['users']['username'], '/users/find/');?>>/td>
        </tr>
   <?php endforeach;?>

  </table>    
</div>
<div class="span3">
    <div class="well" style="margin-top:20px;">
    <?phpecho$this->Form->create('Users', array('url'=>'find'));?>
    <fieldset>
        <legend>Search user</legend>
    </fieldset>
    <?php echo $this->Form-> input('username', array('label'=>'Enter Username', 'class'=>'span12', 'empty'=>true));?>
    <?phpecho$this->Form->end('Search');?>
    </div>
</div>

php cakephp

2022-09-30 19:34

1 Answers

Undefined variable:users has been scolded for not defining the variable users.
The provided code range does not define users until referenced in the foreach statement as shown in the error.

As mentioned above, the warning is probably caused by referring to Undefined Array = Empty Array.

If you want to search and display results on the same page, consider "If you haven't searched anything yet."


2022-09-30 19:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.