【问题标题】:How cakePHP build form datacakePHP 如何构建表单数据
【发布时间】:2014-01-23 18:12:15
【问题描述】:

我创建了一个表格,可以在我的表格中循环并给我一些图片:

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 => '') 
)

)

但是,我怎样才能得到这个?还是一些格式正确的数组?

【问题讨论】:

    标签: arrays forms cakephp input


    【解决方案1】:

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

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

    参考资料:

    【讨论】:

    • 我是这样修改的: Form->input('Gallery.'.$image['medias']['id']. 'title', array('value' => $image['medias']['title']));?> 但它产生: array( 'Gallery' => array( '19title' => 'test', '19description' => '', '20title' => '', '20description' => '', '21title' => '', '21description' => '', '22title' => ' ', '22description' => '' ) )
    • 观看语法...缺少点。
    猜你喜欢
    • 2011-10-23
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    相关资源
    最近更新 更多