我假设在你的控制器中你做了类似的事情:
$this->request->data = $this->Item->find('first', ... );
因此
$data
以子阵列的形式包含关于所选特征的信息,
编辑:
我还假设
Item
哈比姆语
ItemCharacteristic
那么在你看来
$checked_characteristics = Hash::extract($this->data, 'ItemCharacteristic.{n}.id');
foreach($itemCharacteristics as $id => $itemCharacteristic )
{
$checked = in_array($id, $checked_characteristics );
$img = $this->Html->image('cake.icon.png'); // put here the name
// of the icon you want to show
// based on the charateristic
// you are displayng
echo $this->Form->input(
'ItemCharacteristic.ItemCharacteristic.',
array(
'between' => $img,
'label' => $itemCharacteristic,
'value' => $id,
'type' => 'checkbox',
'checked' => $checked
)
);
}
编辑:从你的评论中我理解
$itemCharacteristics
来自
find('list'
)声明。
将其更改为
find('all', array('recursive' => -1));
现在您的代码变成
foreach($itemCharacteristics as $itemCharacteristic )
{
$id = $itemCharacteristic['ItemCharacteristic']['id'];
$icon_name = $itemCharacteristic['ItemCharacteristic']['icon_name']; //or wherever you get your icon path
$img = $this->Html->image($icon_name);
$itemCharacteristicName = $itemCharacteristic['ItemCharacteristic']['name'];
// same as above
}