How to print associated data in the index view

点点圈 提交于 2019-12-12 19:16:05

问题


$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


回答1:


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' => 'user1@sample.com'
            )
    )
);
?> 

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>




回答2:


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

<?php
$array = array(
    0 => array(
        'Users'=>
        array('name' => 'John Doe',
        'email' => 'john@example.com'
        )
    ),
    1 => array(
        'Users'=>array(
        'name' => 'Abs Doe',
        'email' => 'jane@example.com'
        )
    ),
);
?> 

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.

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.




回答3:


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>


来源:https://stackoverflow.com/questions/45971640/how-to-print-associated-data-in-the-index-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!