distromob
distromob

Reputation: 354

How to print associated data in the index view

$subjects = $this->Subjects
    ->find('all', [
        'contain'=> [
            'Users'
        ],
        'fields'=> [
            'Users.username',
            'Users.email'
        ]
    ])
    ->hydrate(false)
    ->toArray();

$this->set('subjects', $subjects);

how can i loop the data in the INDEX view of Subjects controller to display like this image

enter image description here

Upvotes: 2

Views: 105

Answers (3)

Vini App
Vini App

Reputation: 7485

Sample Result from your vardump :

<?php
$array = array(
    0 => array(
        'math'=>40,
        'english'=>40,
        'history'=>40,
        'science'=>40,
        'user_id'=>64
        'user'=>
            array(
            'id' => 6
            'name' => 'User',
            'email' => '[email protected]'
            )
    )
);
?> 

This is the sample code writing based on the vardump you provided:

<table>
    <thead>
    /// Give your table headers
    </thead>
<tbody>
    <?php foreach($subjects as $subject) :?>
        <tr>
            <td><?=$subject['math']?></td>
            <td><?=$subject['english']?></td>
            <td><?=$subject['history']?></td>
            <td><?=$subject['science']?></td>
            <td><?=$subject['user_id']?></td>
            <td><?=$subject['user']['id']?></td>
            <td><?=$subject['user']['name']?></td>
            <td><?=$subject['user']['email']?></td>
        </tr>
    <?php endforeach;?>
</tbody>

Upvotes: 1

502_Geek
502_Geek

Reputation: 2126

I tested it. E.g This is your subject array.

<?php
$array = array(
    0 => array(
        'Users'=>
        array('name' => 'John Doe',
        'email' => '[email protected]'
        )
    ),
    1 => array(
        'Users'=>array(
        'name' => 'Abs Doe',
        'email' => '[email protected]'
        )
    ),
);
?> 

This is loop in your index file.

<table>
    <thead>
        <th>name</th>
        <th>email</th>
    </thead>
    <tbody>
        <?php foreach($array as $subject) :?>
            <tr>
                <td><?=$subject['Users']['name']?></td>
                <td><?=$subject['Users']['email']?></td>
            </tr>
        <?php endforeach;?>
    </tbody>
</table>

It's worked as expected.

enter image description here

I can not give you answer. Because, I don't have access to your environment. But, at least you can get idea which code need amend. Hope this help.

Upvotes: 1

502_Geek
502_Geek

Reputation: 2126

Do play some css first. Hopefully, this code snippet can help you.

You need to do some changes in this.

'fields'=> [
        'Users.username',
        'Users.email',
        //add more fields that you want to display here
]



<table>
    <thead>
         <th>math</th>
         <th>english</th>
         <th>history</th>
         <th>science</th>
         <th>id</th>
         <th>user_id</th>
         <th>username</th>
         <th>email</th>
    </thead>
    <tbody>
        <?php foreach($subjects as $subject) :?>
            <tr>
                <td><?=$subject['Users']['match']?></td>
                <td><?=$subject['Users']['english']?></td>
                <td><?=$subject['Users']['history']?></td>
                <td><?=$subject['Users']['science']?></td>
                <td><?=$subject['Users']['id']?></td>
                <td><?=$subject['Users']['user_id']?></td>
                <td><?=$subject['Users']['username']?></td>
                <td><?=$subject['Users']['email']?></td>
            </tr>
        <?php endforeach;?>
    </tbody>
</table>

Upvotes: 0

Related Questions