gpt4 book ai didi

php - 如何在索引 View 中打印关联数据

转载 作者:行者123 更新时间:2023-11-27 23:11:07 25 4
gpt4 key购买 nike

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

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

我如何循环显示 Subjects Controller 的 INDEX View 中的数据以显示此图像

enter image description here

最佳答案

来自您的 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>

关于php - 如何在索引 View 中打印关联数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45971640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com