【问题标题】:Get post array in dynamic input area jquery to insert batch codeigniter在动态输入区域jquery中获取post数组以插入批处理codeigniter
【发布时间】:2015-12-16 20:29:28
【问题描述】:

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

<?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

 );

我怎样才能做到这一点,我已经两天了,任何帮助都非常感谢。

【问题讨论】:

    标签: javascript php jquery codeigniter


    【解决方案1】:

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

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

    我在这里猜测你是如何组织事情的,但似乎你需要

    1. 遍历您的帖子变量
    2. 找到“listn_item_m”形式的那些
    3. 找到匹配的“kondisi”后变量:listn_kondisi_m
    4. 创建一个插入项,将这些值放入 正确的数据库列。

    我不太确定您的实际命名约定,无法提供有用的示例代码,但大致如下

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

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

    【讨论】:

      猜你喜欢
      • 2017-08-19
      • 2017-04-01
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      相关资源
      最近更新 更多