使用codeigniter插入数组值与foreach [英] Insert array values with foreach using codeigniter

查看:93
本文介绍了使用codeigniter插入数组值与foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a form like below, and it is dynamically adding and removing fields. What I want to achieve is to insert all data from my form while it should not matter how many rows it has already present.

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

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);
}


推荐答案

为我的未来关注的问题。这可能会帮助任何在insert_batch()中有问题或想知道如何在db中使用insert_batch()记录的人。

Just want to post my solution for my question for future concern. this will probably help anyone who has problem in insert_batch() or who wants to know how to use insert_batch() records in db.

在我的模型...



In my model...

$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();
}

我希望它能帮助任何人。

I hope it help anyone.

这篇关于使用codeigniter插入数组值与foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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