在动态输入区域jquery中获取post数组以插入批代码点火器

Get post array in dynamic input area jquery to insert batch codeigniter

本文关键字:数组 插入 点火器 代码 post 获取 输入 动态 区域 jquery      更新时间:2023-09-26

我必须创建一个动态输入,所以我使用这个jquery插件:jquery添加输入区域。这只是我在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。根据在这些文档上插入批处理的代码,它必须具有数组参数。那么,如何将动态输入输入到数组中呢?基于我的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
 );

我怎么能把它变成真的,我有两天的时间,任何帮助都很感激。

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

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

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

  1. 循环浏览post变量
  2. 查找形式为"listn_item_m"的
  3. 查找匹配的"kondisi"post变量:listn_kondisi_m
  4. 创建一个插入项,将这些值放入正确的数据库列

我对您的实际命名约定太不确定了,无法提供有用的示例代码,但它大致类似于

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

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