I have two tables users and subjects
users has this field username,id,email,etc. subjects has this field math,english, id, user_id,
/UsersController.php/
public function grade($id = null)
{
$users = $this->Users->get($id, [
'contain' => [
'Subjects'
]
]);
$this->set('users', $users);
$this->set('_serialize', ['user']);
//pr($users);exit;
}
/*grade.ctp/
[![<div class="users view large-10 medium-9 columns">
<?php
//pr($users);exit;
foreach ($users as $user): ?>
<div class="row">
<div class="large-5 columns strings">
<h6 class="subheader"><?= __('Username') ?></h6>
<p><?= h($user->username) ?></p>
<p><?php echo $this->user->username;?></p>
</div>
<div class="large-2 columns numbers end">
<h6 class="subheader"><?= __('Id') ?></h6>
<p><?= $this->Number->format($user->id) ?></p>
</div>
</div>
<?php endforeach; ?>
</div>][1]][1]
When I click the view grade data below will be output using pr($users)
App\Model\Entity\User Object
(
[username] => Tyra
[password] => 97a8afcf419cc231e1bdcd8584b0a246
[id] => 6
[email] => [email protected]
[profile_pic] => Resource id #196
[destination] =>
[created] =>
[subjects] => Array
(
[0] => App\Model\Entity\Subject Object
(
[math] => 100
[english] => 100
[history] => 100
[science] => 100
[id] => 11
[user_id] => 6
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[repository]] => Subjects
)
)
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[repository]] => Users
)
I try to pr($users); in grade.ctp view I got the data above, which is correct containing the subjects fields. but how to print that in grade.ctp view
Since your data are in object format, you can simply do this:
And to print subjects:
You can try blog tutorials to be more familiar with these in a while..