I got 3 constant rows with few columns like Item,Quantity,Weight...... in a row, in my Interface. When I selected the data in the rows and submit form, only the 3rd row data will be inserted into the database. I want each rows that contain data insert into the database. Any solution for this? Thank You.
Updated:: I had tried to use the PHP array and below is the updated code.But I had stucked at the foreach part, I want to insert the data into the database.
My View:
<tr class="item-details"> <td><span class="rowNumber">1</span></td> <td class=""> <?php $options = array( '' => '~Choose An Item~' ); foreach ($item as $rows){ $options[$rows->id] = $rows->item_name; } $select = array( 'name' => 'item_name[]', 'id' => 'item_name', 'class' => 'form-control' ); echo form_dropdown('item', $options,set_value('item_name'),$select); ?> </td> <td class=""><input type="number" class="item-qty" name="qty[]"/></td> <td><input type="number" name="weight[]" class="weight" /></td> <td><input type="text" name="promo_code[]" value=""/></td> <td><input type="text" name="gp[]" value=""/></td> <td><input type="text" name="discount[]" value=""/></td> <td><input type="text" name="unit_price[]" value=""/></td> <td align="right" class="totalwithouttax">0.00</td> <td align="right" class="totaltax">0.00</td> <td align="right" class="totalamtincltax">0.00</td> </tr><br/> <tr class="item-details"> <td><span class="rowNumber">2</span></td> <td class=""> <?php $options = array( '' => '~Choose An Item~' ); foreach ($item as $rows){ $options[$rows->id] = $rows->item_name; } $select = array( 'name' => 'item_name[]', 'id' => 'item_name', 'class' => 'form-control' ); echo form_dropdown('item', $options,set_value('item_name'),$select); ?> </td> <td class=""><input type="number" class="item-qty" name="qty[]"/></td> <td><input type="number" name="weight[]" class="weight" /></td> <td><input type="text" name="promo_code[]" value=""/></td> <td><input type="text" name="gp[]" value=""/></td> <td><input type="text" name="discount[]" value=""/></td> <td><input type="text" name="unit_price[]" value=""/></td> <td align="right" class="totalwithouttax">0.00</td> <td align="right" class="totaltax">0.00</td> <td align="right" class="totalamtincltax">0.00</td> </tr><br/> <tr class="item-details"> <td><span class="rowNumber">3</span></td> <td class=""> <?php $options = array( '' => '~Choose An Item~' ); foreach ($item as $rows){ $options[$rows->id] = $rows->item_name; } $select = array( 'name' => 'item_name[]', 'id' => 'item_name', 'class' => 'form-control' ); echo form_dropdown('item', $options,set_value('item_name'),$select); ?> </td> <td class=""><input type="number" class="item-qty" name="qty[]"/></td> <td><input type="number" name="weight[]" class="weight" /></td> <td><input type="text" name="promo_code[]" value=""/></td> <td><input type="text" name="gp[]" value=""/></td> <td><input type="text" name="discount[]" value=""/></td> <td><input type="text" name="unit_price[]" value=""/></td> <td align="right" class="totalwithouttax">0.00</td> <td align="right" class="totaltax">0.00</td> <td align="right" class="totalamtincltax">0.00</td> </tr><br/> Controller:
//***************** ORDER ITEM ******************** $orderID = 1; $productId = $this->input->post('product_id'); $itemName = $_POST['item_name'];//$this->input->post('item_name'); $qty = $this-> $_POST['qty'];//input->post('qty'); $weight = $this-> $_POST['weight'];//input->post('weight'); $unitPrice = $_POST['unit_price'];//$this->input->post('unit_price'); $transportationPrice = $this->input->post('transportation_price'); $itemType = $this->input->post('item_type'); $gp = $this-> $_POST['gp'];//input->post('gp'); $tax = $this->input->post('tax'); $amtInclTax = $this->input->post('amt_incl_tax'); $amtWithoutTax = $this->input->post('amt_without_tax'); $taxCode = $this->input->post('tax_code'); $orderItems = array ( 'order_id' => $orderHeaderId , 'product_id' => 7 , 'item' => $itemName , 'qty' => $qty , 'weight' => $weight , 'unit_price' => $unitPrice , 'transportation_price' => 0 , 'item_type' => 'GOLD' , 'gp' => $gp, 'discount' => $discount, 'amt_without_tax' => 10, 'tax' => 20 , 'amt_incl_tax' => 30, 'tax_code' => 40 , ); echo '<pre>'; print_r($orderItems); echo '</pre>'; foreach($itemName as $key => $iN){ //stuck at here } $orderHeaderId = $this->order->InsertItemData($orderHeaderId,$orderHeader,$orderItems); Model:
public function InsertItemData($orderHeaderId,$orderHeader,$orderItems){ //take order_id from 'order' table //from InsertData(model) function $orderHeaderId = $this->InsertData($orderHeader); // Inserting Order Item if($orderItems){ $this->db->insert_batch('order_items', $orderItems); } }
gp[]Take a look here stackoverflow.com/questions/3314567/…