简体   繁体   English

cakephp将多个动态生成的下拉列表保存到数据库

[英]cakephp saving multiple dynamic generated drop-down list to database

I have two drop down list and a textbox which can be dynamically added to my form with a button click as many times as possible, but my problem is the array of data from the dynamic form elements are not saving to my database table at a single submit. 我有两个下拉列表和一个文本框,可以通过多次单击将其动态添加到表单中,但是我的问题是来自动态表单元素的数据数组无法一次保存到数据库表中提交。 Below is the structure of the array on debug. 下面是调试数组的结构。

* current array * * 当前数组 *

array(
    'DrugTransaction' => array(
        'drug_type_id' => array(
            (int) 0 => '1',
            (int) 1 => '1'
        ),
        'drug_id' => array(
            (int) 0 => '1',
            (int) 1 => '2'
        ),
        'quantity' => array(
            (int) 0 => '15',
            (int) 1 => '21'
        )
    )
)

I want the array to look like the sample below 我希望数组看起来像下面的示例

Array ( 
    [0] => Array ( 
        [drug_type_id] => 1 
        [drug_id] => 2 
        [quantity] => 14 

    )  
    [1] => Array ( 
        [drug_type_id] => 3 
        [drug_id] => 4 
        [quantity] => 100      
    )  
)

try this code 试试这个代码

$result = array();

foreach($yourArray['DrugTransaction'] as $key => $subArray)
{

    foreach($subArray as $index => $value)
    {
        $result[$index][$key] = $value;
    }

}

edit: I think you also want id set to null, so you have to do something like: 编辑:我认为您还希望将id设置为null,因此您必须执行以下操作:

foreach($result as $key => $value)
{
    $result[$key]['id'] = null;
    $result[$key]['supplydate'] = '2014-03-31';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM