CakePHP:渲染未定义的索引

时间:2011-02-07 19:06:31

标签: php cakephp cakephp-1.3

我调试了视图页面只是为了确认数据是否已正确收集。事实上,我有这个:

 [Friend] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [username] => James_Baker
                            [password] => 7pooplllLLKMKMKKkss09koskld1d9b5e6
                            [first_name] => James
                            [last_name] => Baker
                            [email] => D-W-James@yahoo.com
                            [group_id] => 2
                            [created] => 2011-01-10 08:25:52
                            [modified] => 2011-02-04 17:30:47
                            [slug] => 
                 ....
                 ....

当我尝试在我的视图页面上显示时,从我的view.ctp这里是我目前使用的内容:

..
<?php foreach ($users as $user): ?>

    <tr>
        <td><?php echo $user['Friend']['username']; ?></td>

    </tr>
<?php endforeach; ?>
..

但是我收到了这个错误:

Undefined index: username

有人可以根据我的调试代码修复错误吗?我正在尝试输出/回显朋友的用户名。

2 个答案:

答案 0 :(得分:5)

调用此值的正确键为$user['Friend'][0]['username']

在开始每个循环后调试使用var_dump($user); exit;

<?php foreach ($users as $user): ?>
     <h1>User Friends</h1>
     <?php foreach ($user['Friend'] as $friend): ?>
         <?php echo $friend['username']; ?>
     <?php endforeach; ?>
<?php endforeach; ?>

答案 1 :(得分:0)

听起来你想要$ user ['Friend'] [0] ['username']