LoginSignup
1
0

More than 3 years have passed since last update.

Cakephp3で取得したオブジェクトを元にチェックボックスを作る

Posted at

Cakephpでfindした結果でcheckbox作りたい時、どうすればよいかわからなかったが
色々やってこの方法に落ち着いた。
toArray()を使う方法もありましたが、
json_encode と json_decode を使う方法に落ち着きました。
シンプルだけど疑問が残ります。

php

//$usersオブジェクトをjsonに
<?php $user_array =  json_decode(json_encode($users), true);?>

//フォーム生成
<?= $this->Form->create($blog) ?>
<?php echo $this->Form->control('roomname');?>

<?php foreach ($user_array as $user_data): ?>
<?php echo $this->Form->control('user[]', array(
    'label' => $user_data['users']['username'],
    'value' => $user_data['users']['id'],
    'id' => 'user_id_'.$user_data['users']['id'],
    'type' => 'checkbox'
));?>
<?php endforeach; ?>

<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0