instead of manipulating $_POST in controller manipulate it in model like this
Your controller
$this->load->model('buy_product_model'); $this->buy_product_model->add_product($this->input->post()); EDIT
Make sure the form fields in html form are in array form like this
<input type="text" name="code[]" /> Your model
public function buy_product_model($postdata){ $code=$postdata['code']; $rate=$postdata['rate']; $quantity=$postdata['quantity']; //$total=$postdata['rate']*$postdata['quantity'];extract($postdata); $count = count($postdata['code']$code); for($i=0; $i<$count; $i++) { $data = array( 'shop'=>$shop->$this->input->post('shop'), 'code' => $code[$i], 'rate' => $rate[$i], 'quantity' => $quantity[$i], 'total' =>($rate[$i]*$quantity[$i]) ); $this->db->insert('[YOUR TABLE]', $data); } } MAKE SURE TO CHANGE [YOUR TABLE] TO YOURS
add rest of the code you have in method add_product of your model and iterate through a loop as you have done in your controller.
Edit:
Please check 'shop'=>$shop->$this->input->post('shop') part in your loop