如何在索引视图中打印关联的数据 [英] How to print associated data in the index view

查看:172
本文介绍了如何在索引视图中打印关联的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如何循环Subjects控制器的INDEX视图中的数据以显示此图像

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

推荐答案

您的vardump结果:

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'
            )
    )
);
?> 

这是根据您提供的vardump编写的示例代码:

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>

这篇关于如何在索引视图中打印关联的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆