0

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.

2 Answers 2

5

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; ?>
Sign up to request clarification or add additional context in comments.

1 Comment

to debug, better use pr() or debug()
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.