在动态输入区域jquery中获取post数组以插入批处理codeigniter

时间:2015-12-16 20:29:28

标签: javascript php jquery codeigniter

我必须创建一个动态输入,所以我使用这个jquery插件 : jquery add input area。这只是我在html中的输入(对不起,它有点长代码coz bootstrap标签):

<?php echo form_open_multipart('', array('id' => 'upload', 'enctype' => "multipart/form-data")); ?>
        <div class="box-body no-padding">
            <div class="form-group col-sm-3">
                <label for="container">Container No</label>
                <input type="text" name="container" id="container" class="form-control"/>
            </div>


      //This is the dynamic input Area, I have two.
             <div class="form-group col-sm-6">
                <label for="cek_kondisi">Cek Kondisi Top</label>
                <table id="list2" class="table table-bordered">
                    <thead>
                        <tr>
                            <th style="width: 70%">Item</th>
                            <th style="width: 20%">Kondisi</th>
                            <th style="width: 10%">Act</th>
                        </tr>
                    </thead>

                    <tbody>
                        <tr class="list2_var">
                            <td>
                                <select class="form-control" name="list2_item_0" id="list2_name_0">
                                    <option>Pilih jenis kerusakan... </option>
                                    <?php
                                    foreach ($top_item as $v) {
                                        echo '<option value =' . $v->ID_ITEM_INSPECTION . ' >' . $v->NOMOR_ITEM_INSPECTION . '    - ' . $v->NAMA_ITEM_INSPECTION . '</option>';
                                    }
                                    ?>
                                </select>

                            </td>
                            <td>
                                <select class="form-control" name="list2_kondisi_1" id="list2_name_1">
                                    <option></option>
                                    <?php
                                    foreach ($detail_condition as $v) {
                                        echo '<option value =' . $v->ID_ITEM . ' >' . $v->ALIAS . '    - ' . $v->NAME_ITEM . '</option>';
                                    }
                                    ?>
                                </select>
                            </td>

                            <td class="del_area04"><button class="list2_del btn btn-block btn-danger" type="button"><i class="fa fa-trash-o"></i></button></td>
                        </tr>
                    </tbody>
                </table>

                <a href="javascript:void(0)" class="list2_add btn btn-success">Add</a><br>
            </div>

            <div class="form-group col-sm-6">
                <label for="cek_kondisi">Cek Kondisi Bottom</label>
                <table id="list3" class="table table-bordered">
                    <thead>
                        <tr>
                            <th style="width: 70%">Item</th>
                            <th style="width: 20%">Kondisi</th>
                            <th style="width: 10%">Act</th>
                        </tr>
                    </thead>

                    <tbody>
                        <tr class="list3_var">
                            <td>
                                <select class="form-control" name="list3_item_0" id="list3_name_0">
                                    <option>Pilih jenis kerusakan...</option>
                                    <?php
                                    foreach ($bottom_item as $v) {
                                        echo '<option value =' . $v->ID_ITEM_INSPECTION . ' >' . $v->NOMOR_ITEM_INSPECTION . '    - ' . $v->NAMA_ITEM_INSPECTION . '</option>';
                                    }
                                    ?>
                                </select>

                            </td>
                            <td>
                                <select class="form-control" name="list3_kondisi_1" id="list3_name_1">
                                    <option></option>
                                    <?php
                                    foreach ($detail_condition as $v) {
                                        echo '<option value =' . $v->ID_ITEM . ' >' . $v->ALIAS . '    - ' . $v->NAME_ITEM . '</option>';
                                    }
                                    ?>
                                </select>
                            </td>

                            <td class="del_area04"><button class="list3_del btn btn-block btn-danger" type="button"><i class="fa fa-trash-o"></i></button></td>
                        </tr>
                    </tbody>
                </table>

                <a href="javascript:void(0)" class="list3_add btn btn-success">Add</a><br>
            </div>
            <div class="form-group col-sm-12">
                <label>Comments</label>
                <textarea class="form-control" rows="7" placeholder="Masukkan comment Anda" name="comments"></textarea>
            </div>
        </div><!-- /.box-body -->

        <div class="box-footer">
            <button type="submit" class="btn btn-primary btn-block">Submit</button>
        </div>
        <?php echo form_close(); ?>

这是jquery

$('#list2').addInputArea({
    after_add: function () {

    }
});
$('#list3').addInputArea();

使用这样的控制器:

public function add_inspection() {      
    echo ("<pre>");
    print_r($this->input->post());
}

我有这样的数组:

Array
(
[condition] => Array
    (
        [0] => 1
    )

[container] => EOLU-123456-7
[cleaning] => Y
[owner] => Eagletainer
[last_cargo] => 9
[vessel] => MV.WAN HAI 171
[insulation] => 2
[tare] => 1200
[gross] => 3000
[capacity] => 3000
[unit_type] => IMO 1
[date_of_manu] => 22071989
[name_manu] => PT.Test
[last25] => 22071989
[cert25] => Test
[last5] => 22071989
[cert5] => Test
[list2_item_0] => 1
[list2_kondisi_0] => 9
[list2_item_1] => 2
[list2_kondisi_1] => 
[list3_item_0] => 12
[list3_kondisi_0] => 9
[list3_item_1] => 13
[list3_kondisi_1] => 
[comments] => Test Comment

为了最小化我的数据库服务器的执行,我想使用insert_batch。根据在docs上插入批处理的代码,它必须具有数组参数。 那么如何才能将动态输入转换为数组呢?基于我的Post Array,这是动态输入:

[list2_item_0] => 1
[list2_kondisi_0] => 9
[list2_item_1] => 2
[list2_kondisi_1] => 
[list3_item_0] => 12
[list3_kondisi_0] => 9
[list3_item_1] => 13
[list3_kondisi_1] => 

我需要:

 $data = array(
    array(
            [list2_item_0] => 1
            [list2_kondisi_0] => 9
    ),
    array(
            [list2_item_1] => 2
            [list2_kondisi_1] => 
    )
   //SO ON, SO ON

 );

我怎样才能做到这一点,我有两天的堆叠,任何帮助它如此欣赏。

1 个答案:

答案 0 :(得分:0)

首先,要传递给insert_batch的数据数组需要使用数据库中的列名。我对你的命名约定感到有些困惑,但结果将是这样的:

 $data = array(
    array(
            'column1' => 1
            'column2' => 9
    ),
    array(
            'column1' => 2
            'column2' => 
    )
   //SO ON, SO ON

 );

我在这里猜一下你如何组织事情,但似乎你需要

  1. 遍历您的帖子变量
  2. 找到表格&#34; listn_item_m&#34;
  3. 找到&kondisi&#39;匹配的post变量:listn_kondisi_m
  4. 创建一个插入项,将这些值放入 正确的数据库列。
  5. 我太不确定您的实际命名约定,以提供有用的示例代码,但它大致类似于

    $data[] = array('column1' => $this->input->post($item), 'column2' => $this->input->post($kondisi);
    

    其中$item是您在步骤2中找到的帖子变量的名称,而$kondisi将是您在步骤3中找到的匹配变量。