CakePHP:渲染未定义的索引

发布于 2024-10-16 10:06:47 字数 1268 浏览 2 评论 0原文

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

 [Friend] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [username] => James_Baker
                            [password] => 7pooplllLLKMKMKKkss09koskld1d9b5e6
                            [first_name] => James
                            [last_name] => Baker
                            [email] => [email protected]
                            [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

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

I've debugged the view page just to confirm the data were properly collected. And indeed I have this:

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

As I tried displaying on my view page, from my view.ctp here's what I currently use:

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

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

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

but I'm getting this error:

Undefined index: username

Can someone fix the error based on the debug code I have ? I'm trying to output/echo the friend's username.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贪了杯 2024-10-23 10:06:47

调用此值的正确键是 $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; ?>

Correct key to call this value is $user['Friend'][0]['username']

To debug use var_dump($user); exit; just after you start every loop.

<?php foreach ($users as $user): ?>
     <h1>User Friends</h1>
     <?php foreach ($user['Friend'] as $friend): ?>
         <?php echo $friend['username']; ?>
     <?php endforeach; ?>
<?php endforeach; ?>
时光礼记 2024-10-23 10:06:47

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

sounds like you want $user['Friend'][0]['username']

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文