cakePHP如何构建表单数据

时间:2014-01-23 18:12:15

标签: arrays forms cakephp input

我创建了一个在我的表格中循环的表单并给我一些图像:

id1: - >图片路径,标题等 id2: - >图像路径标题等

我以这种方式创建了我的观点:

<div>
<?php echo $this->Form->create('Gallery', 
                array( 'action' => 'save')); ?>
<table cellpadding="0" cellspacing="0">
    <?php foreach($images as $image): ?>
    <tr>
        <td>
            <?php echo $this->PhpThumb->thumbnail($image['medias']['file'], array('w' => 75, 'h' => 75)); ?>
        </td>
        <td><?php echo $this->Form->input('title'.$image['medias']['id'], array('value' => $image['medias']['title']));?></td>
        <td><?php echo $this->Form->input('description'.$image['medias']['id'], array('value' => $image['medias']['description']));?></td>
    </tr>
    <?php endforeach; ?>
</table>
<?php echo $this->Form->end(__('Submit')); ?>

提交后,我有一个这样的数组:

array(
'Gallery' => array(
    'title19' => 'test',
    'description19' => '',
    'title20' => '',
    'description20' => '',
    'title21' => '',
    'description21' => '',
    'title22' => '',
    'description22' => ''
)

当然,我不能使用save()方法,因为数组格式不正确。我认为正确的格式应该是:

array(
'Gallery' => array(
    array('id' => 19,
          'title => 'test',
          'description => ''),
    array( 'id' =>20,
            title => '',
            description => '') 
)

但是,我怎么能得到这个?或者一些格式正确的数组?

1 个答案:

答案 0 :(得分:0)

当你遍历它们时,将迭代器或id放在正确的位置:

$this->Form->input('Model.' . $i . 'field');
$this->Form->input('Model.' . $i . 'other_field');

参考文献: