javascript - 获取动态输入区jquery中的post数组插入批量codeigniter

标签 javascript php jquery codeigniter

我必须创建一个动态输入,所以我使用这个 jquery 插件 : jquery add input area 。这只是我在 html 中输入的一部分(抱歉,代码有点长,因为 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();

使用这样的 Controller :

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 上插入批处理的代码,它必须具有数组参数。 那么,如何将动态输入获取到数组中呢?基于我的帖子数组,这是动态输入:

[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. 循环遍历帖子变量
  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 中找到的匹配变量。

关于javascript - 获取动态输入区jquery中的post数组插入批量codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34321506/

相关文章:

javascript - 验证至少已选择一个选项

javascript - JQuery 和 Html 忽略引号

javascript - DC.js |我怎样才能让.title(工具提示)在箱线图上工作?

javascript - 如何让 Google 文件选取器脚本在公开共享的 Google 工作表上启动?

JavaScript 动态回调函数

javascript - 取消压缩/解压缩 JavaScript

jquery - 在 jQuery 中展开折叠的 div 时如何折叠所有已展开的 div?

javascript - arc.centroid 在 D3 中返回 (NaN, NaN)

安装composer时php版本错误

php - 选择带有 sport=padel 的所有行