1

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:

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

I have a controller like:

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

}

And a model like:

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 Answers 2

2
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);
}
Sign up to request clarification or add additional context in comments.

6 Comments

hi @Aishish i receive error You must use the "set" method to update an entry
sorry i receive error again Message: Use of undefined constant i - assumed 'i'
use $data[$i]; instead of $data[i]
Message: Undefined variable: data sorry but I confused now
post the new code you using i will help you correcting that
|
0

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

and in my model

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.