dongshi1934 2011-02-07 19:06
浏览 8
已采纳

CakePHP:渲染未定义的索引

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] => D-W-James@yahoo.com
                            [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.

  • 写回答

2条回答 默认 最新

  • dougezhua0017 2011-02-07 19:14
    关注

    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; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度