使用codeigniter使用foreach插入数组值

时间:2014-06-21 10:59:32

标签: php codeigniter foreach

我有一个如下表单,它是动态添加和删除字段。我想要实现的是插入表单中的所有数据,而不管它已经存在多少行。

<tr>
<td><input name="model[]" type="text" class="input-large" id="A1" value="" /></td>
<td><input name="color[]" type="text" class="input-small" id="Color1" value="" /></td>
<td><input name="price[]" type="text" class="input-small" id="B1" value="" data-format="0,0[.]00" /></td>
<td><input name="quantity[]" type="text" class="input-small" id="C1" value="" data-format="0" /></td>
<td><input name="addon[]" type="text" class="input-small" id="D1" value="" data-format="0,0[.]00" /></td>
<td><input name="discount[]" type="text" class="input-small" id="E1" value="" data-format="0,0[.]00" /></td>
<td><input name="total[]" type="text" class="input-small" id="F1" value="" data-formula="($B1*$C1)+($D1-$E1)" readonly /></td>
<td><input type="button" value="remove" class="btn-remove btn btn-mini btn-danger" /></td>
</tr>

的javascript:

<script type="text/javascript">
var currentRow = 1;
$(document).ready(function(){
    $('#calx').calx();

    $('#add_item').click(function(){
        var $calx = $('#calx');
        currentRow++;

        $calx.append('<tr>\
            <td><input type="text" name="model[]" class="input-large" id="A'+currentRow+'" value="" /></td>\
            <td><input type="text" name="color[]" class="input-small" id="Color1'+currentRow+'" value="" /></td>\
            <td><input type="text" name="price[]" class="input-small" id="B'+currentRow+'" value="" data-format="0,0[.]00" /></td>\
            <td><input type="text" name="quantity[]" class="input-small" id="C'+currentRow+'" value="" data-format="0" /></td>\
            <td><input type="text" name="addon[]" class="input-small" id="D'+currentRow+'" value="" data-format="0,0[.]00" /></td>\
            <td><input type="text" name="discount[]" class="input-small" id="E'+currentRow+'" value="" data-format="0,0[.]00" /></td>\
            <td><input type="text" name="total[]" class="input-small" id="F'+currentRow+'" value="" data-format="" data-formula="($B'+currentRow+'*$C'+currentRow+'+$D'+currentRow+'-$E'+currentRow+')" /></td>\
            <td><input type="button" value="remove" class="btn-remove btn btn-mini btn-danger" /></td>\
        </tr>');

        //update total formula
        $('#G1').attr('data-formula','SUM($F1,$F'+currentRow+')');
        $calx.calx('refresh');
    });

    $('#calx').on('click', '.btn-remove', function(){
        $(this).parent().parent().remove();
        $('#calx').calx('refresh');
    });
});
</script>

我有一个控制器,如:

    public function add_transactions(){
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');

    $this->form_validation->set_rules('model[]', 'Item Name or Model', 'trim|required|xss_clean');
    $this->form_validation->set_rules('color[]', 'Color', 'trim|required|xss_clean');
    $this->form_validation->set_rules('price[]', 'Item Price', 'trim|required|xss_clean');
    $this->form_validation->set_rules('quantity[]', 'Quantity', 'trim|required|xss_clean');
    $this->form_validation->set_rules('addon[]', 'Add-on', 'trim|required|xss_clean');
    $this->form_validation->set_rules('discount[]', 'Discount', 'trim|required|xss_clean');
    $this->form_validation->set_rules('total[]', 'Unit cost or Amount', 'trim|required|xss_clean');


    if($this->form_validation->run() == FALSE){

        $code = $this->input->post('code');

        $this->load->model('purchase_order');
        $this->load->model('supplier');
        $data['result'] = $this->purchase_order->getPurchaseOrderByID($code);
        $data['results'] = $this->supplier->SupplierList();
        $data['main_content'] = 'purchase_order_pricing.php';
        $this->load->view('dashboard',$data);                       

    } else {

        $this->load->model('purchase_order');
        $result = $this->purchase_order->AddPurchaseOrder($data);
        if(!$result['error'])
        {
            $this->session->set_flashdata('flashSuccess', 'Purchase Order transaction has been updated.');
            redirect('home/purchase_order_view', 'refresh');
            $data['main_content'] = 'purchase_order_view.php';
            $this->load->view('dashboard',$data);                       
        } 
        else 
        {
            $this->session->set_flashdata('flashSuccess', 'The server is busy please try again later.');
            redirect('home/purchase_order_view', 'refresh');
            $data['main_content'] = 'purchase_order_view.php';
            $this->load->view('dashboard',$data);       
        }               
    }       

}

这样的模型:

public function AddPurchaseOrder($data){



$data =array();
    for($i=0; $i<sizeof($data); $i++) {
        $data[$i] = array(
            'model' => $model[$i], 
            'color' => $color[$i],
            'price' => $price[$i],
            'quantity' => $quantity[$i],
            'addon'=>$addon[$i],
            'discount'=>$discount[$i],
            'total'=>$total[$i],
            'code'=>$code[$i] 
        );
    }
$this->db->insert('purchase_order_info',$data);
}

2 个答案:

答案 0 :(得分:1)

public function AddPurchaseOrder ($data)
{
    $data =array();
    for($i=0; $i < $count; $i++) 
    {
        $data[$i] = array(
        'model' => $model[$i], 
        'color' => $color[$i],
        'price' => $price[$i],
        'quantity' => $quantity[$i],
        'addon'=>$addon[$i],
        'discount'=>$discount[$i],
        'total'=>$total[$i] 
        );
    }

    $this->db->insert_batch('table_name_here', $data);
}

答案 1 :(得分:0)

只想发布我的问题解决方案以备将来关注。这可能会帮助任何在insert_batch()中遇到问题或者想知道如何在db中使用insert_batch()记录的人。

在我的模特中......

$model = $this->input->post('model');
$color = $this->input->post('color');
$price = $this->input->post('price');
$quantity = $this->input->post('quantity');
$addon = $this->input->post('addon');
$discount = $this->input->post('discount');
$total = $this->input->post('total');

$orders = array();

for ($i=0; $i < count($model); $i++) { 

        $orders[] = array( 'id'=>null,
        'model' => $model[$i],
        'color' => $color[$i],
        'price' => $price[$i],
        'quantity' => $quantity[$i],
        'addon' => $addon[$i],
        'discount' => $discount[$i],
        'total' => $total[$i]   
        );

    } 

$this->load->model('purchase_order');
$result = $this->purchase_order->AddPurchaseOrder($orders);

在我的模特中

public function AddPurchaseOrder($orders){

$this->db->insert_batch('purchase_order_info', $orders); 
print '<pre>';
print_r($orders);
print '</pre>';
die();
}

我希望它可以帮助任何人。