如何在索引视图中打印关联数据
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);
如何循环显示 Subjects 控制器的 INDEX 视图中的数据,以像这张图片那样显示
先玩一些 css。希望此代码片段可以帮助您。
您需要对此进行一些更改。
'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>
我测试过了。例如,这是您的主题数组。
<?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>
它按预期工作。
我无法给你答案。因为,我无权访问您的环境。但是,至少你可以知道哪些代码需要修改。
希望这有帮助。
来自您的 var_dump 的示例结果:
<?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'
)
)
);
?>
这是根据您提供的var_dump编写的示例代码:
<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>
$subjects = $this->Subjects
->find('all', [
'contain'=> [
'Users'
],
'fields'=> [
'Users.username',
'Users.email'
]
])
->hydrate(false)
->toArray();
$this->set('subjects', $subjects);
如何循环显示 Subjects 控制器的 INDEX 视图中的数据,以像这张图片那样显示
先玩一些 css。希望此代码片段可以帮助您。
您需要对此进行一些更改。
'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>
我测试过了。例如,这是您的主题数组。
<?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>
它按预期工作。
我无法给你答案。因为,我无权访问您的环境。但是,至少你可以知道哪些代码需要修改。 希望这有帮助。
来自您的 var_dump 的示例结果:
<?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'
)
)
);
?>
这是根据您提供的var_dump编写的示例代码:
<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>