Codeigniter通过发布数组循环 [英] Codeigniter loop through post array

查看:34
本文介绍了Codeigniter通过发布数组循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这种形式:

    <form id="stepform" action="#" method="post">
        <fieldset>
        <legend>Step #1</legend>
        <label>Title</label>
        <input type="hidden" name="csrf_modo" value="b94961394f8e6f7efaa4e37ca9007822">
        <input type="text" name="field[]" class="input-xlarge">
        <label>Body</label>
        <textarea class="input-xlarge" name="field[]"></textarea>
        </fieldset>
    </form>

当用户单击按钮时,jquery会动态追加另外两个完全相同的字段:

When user clicks a button jquery dynamically appends another two exactly same fields:

 count = 2;
$("#addstep").click(function(){


    $('#stepform').append('<legend>Step #' + (count++) + '</legend>');
    $('#stepform').append('<label>Title</label><input type="text" name="field[]" class="input-xlarge">');
    $('#stepform').append('<label>Body</label><textarea class="input-xlarge" name="field[]"></textarea>');
    $('#addstep').scrollintoview();
    return false;

});

您可以看到一个步骤包含2个字段,当用户单击按钮步骤时,该步骤会递增,并向该步骤添加另外2个字段,依此类推...之后,我通过ajax请求将数据发送到控制器.现在,我陷入了实际查询,该查询应该为每一步插入新行.我怎么能做到这一点?

As you can see one step has 2 fields, when user clicks on the button step increments and adds another 2 fields to that step and so on... After that i send data to the controller via ajax request. Now i'm stuck on the actual query which should insert new row for every step. How could i accomplish this?

顺便说一句,我正在使用codeigniter,它是绑定查询:

Btw i'm using codeigniter and it's bind queries:

$this->db->query($sql, $data);

推荐答案

更新

我更正了文本区域和输入字段之间的差异的处理.旁注:整个Controller逻辑都属于一个模型.出于简化的原因,我只是将其放入此处的控制器中.

I corrected the handling of the difference between textareas and input fields. Sidenote: The whole Controller logic belongs into a model. I just put it into the Controller here for simplification reasons.

HTML

<form id="stepform" action="#" method="post">
    <fieldset>
    <legend>Step #1</legend>
    <label>Title</label>
    <input type="hidden" name="csrf_modo" value="b94961394f8e6f7efaa4e37ca9007822">
    <input type="text" name="field[input][]" class="input-xlarge">
    <label>Body</label>
    <textarea class="input-xlarge" name="field[textarea][]"></textarea>
    </fieldset>
</form>

JS

count = 2;
$("#addstep").click(function(){


    $('#stepform').append('<legend>Step #' + (count++) + '</legend>');
    $('#stepform').append('<label>Title</label><input type="text" name="field[input][]" class="input-xlarge">');
    $('#stepform').append('<label>Body</label><textarea class="input-xlarge" name="field[textarea][]"></textarea>');
    $('#addstep').scrollintoview();
    return false;

});

PHP

class SomeController extends MY_Controller{

    public function process_request()
    {
        $insert_data = array();
        $field_data = $this->input->post('field');

        for($i = 0; $i < count($field_data['input']); $i++)
        {
            $insert_data[] = array(
                'db_col_name_input' => $field_data['input'][$i],
                'db_col_name_textarea' => $field_data['textarea'][$i]
            );
        }

        $this->db->insert_batch('my_table', $insert_data);

    }

}

旧答案:

由于要将方括号添加到输入字段名称中,因此您将获得一个数组,其中包含具有该名称的所有字段的值.因此,您可以使用foreach循环遍历它们,并将所有值存储在数组中,然后使用CodeIgniters insert_batch()方法同时插入多个数据.

Since you're appending square brackets to your input fields name you will get an array with the values of all fields that are having this name. So you can go trough them with a foreach loop and store all the values in an array and use CodeIgniters insert_batch() Method to insert multiple data at the same time.

class SomeController extends MY_Controller{

    public function process_request()
    {
        $insert_data = array();

        foreach($this->input->post('field') AS $field)
        {
            $insert_data[] = array(
                'db_col_name' => $field
            )
        }

        $this->db->insert_batch('my_table', $insert_data);

    }

}

这篇关于Codeigniter通过发布数组循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆