【问题标题】:How to print associated data in the index view如何在索引视图中打印关联数据
【发布时间】:2017-08-31 00:49:06
【问题描述】:
$subjects = $this->Subjects
    ->find('all', [
        'contain'=> [
            'Users'
        ],
        'fields'=> [
            'Users.username',
            'Users.email'
        ]
    ])
    ->hydrate(false)
    ->toArray();

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

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

【问题讨论】:

    标签: php html css cakephp cakephp-3.0


    【解决方案1】:

    来自您的 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 编写的示例代码:

    <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>
    

    【讨论】:

    • 您可以发布您的查询吗?你在检索 id 和 user_id 吗? id 和 user_id 来自哪个表?
    • 查看图片查询ibb.co/mZ0QP5 id 来自users 表,user_id 在subjects 表中
    • id 和 user_id 在你建议的代码中具有相同的值,原始值i.stack.imgur.com/4vHGN.png
    • 但是在给定的图像中它们是不同的
    • 你的意思是什么图片?这是我在命令行中查询的原始数据i.stack.imgur.com/4vHGN.png
    【解决方案2】:

    我测试过了。例如,这是您的主题数组。

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

    这是索引文件中的循环。

    <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>
    

    它按预期工作。

    我不能给你答案。因为,我无权访问您的环境。但是,至少你可以知道哪些代码需要修改。 希望这有帮助。

    【讨论】:

    • 我错过了什么?这是错误通知 (8): Undefined variable: array [APP/Template\Subjects\index.ctp, line 27] 警告 (2): Invalid argument provided for foreach() [APP/Template\Subjects\index.ctp,第 27 行]
    • 这是测试数组。你需要用你的数组替换。
    • 我将 araay 替换为 $subjects =$subject['Users']['username'] ?> =$subject['Users']['email']?> 未定义索引:用户 [APP/模板\ Subjects\index.ctp,第 29 行,第 30 行,第 31 行,第 32 行等
    • 在视图文件中尝试 var_dump 到 $subjects。然后,使用打印结果更新您的问题。这会很有帮助。
    • 它将显示数组的所有详细信息和值参见图片ibb.co/kKas45
    【解决方案3】:

    先玩一些css。希望这段代码 sn-p 可以帮助你。

    您需要对此进行一些更改。

    '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>
    

    【讨论】:

    • 感谢您的回答,但仍然不会打印用户名和电子邮件
    • 意思,可以打印其他字段吗?
    • 记住用户名和电子邮件字段在用户表中,是的,除了用户名和电子邮件之外的所有数据都会打印
    • 请尝试=$subject['Users']['username']?>
    • 检查更新的答案。但是,您也需要修改您的查询。您刚刚在查询中仅选择了两个字段,例如电子邮件和用户名。您需要添加要在视图文件中显示的其他字段。
    猜你喜欢
    • 2013-11-14
    • 2017-03-28
    • 2011-01-20
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    相关资源
    最近更新 更多