CakePHP: Rendering an undefined index
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
开发者_StackOverflow [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.
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; ?>
sounds like you want $user['Friend'][0]['username']
精彩评论