0

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

}

4
  • Can you post your code on your controller? Commented Feb 26, 2019 at 1:18
  • I'm not sur but it looklike you have the same name for each input... So basically each row are overwrited by the next one, until the third row. You should use different name or use array like that gp[] Take a look here stackoverflow.com/questions/3314567/… Commented Feb 26, 2019 at 1:27
  • @Roshan I had updated my controller Commented Feb 26, 2019 at 1:43
  • @Eiji Thanks, I will take a look for this solution ! Commented Feb 26, 2019 at 1:44

2 Answers 2

0

First make your all 3 portion of HTML form input name array like

 <input type="number" class="item-qty" name="qty[]"/>
 <input type="number" name="weight" class="weight[]" />
 ...

Then loop your data array as

$qty = $this->input->post('qty', TRUE);
$weight = $this->input->post('weight', TRUE);
$orderItems = array();
for ($i = 0; $i < count($qty); $i++) {
   if ($qty[$i]) {
      $orderItems[] = array (
                          'order_id' =>  $orderHeaderId ,
                          'product_id' =>  7 ,
                          'item' => 'TEST'  ,
                          'qty' =>  $qty[$i] ,
                          'weight' => $weight[$i]
       ...
       );
   }
 }

And now you may use insert_batch method as

if ($orderItems) {
      $this->db->insert_batch('table_name', $orderItems);
}
Sign up to request clarification or add additional context in comments.

5 Comments

I selected data in 3 rows, but it inserted 6 rows in database. For example, Array[0],Array[0] Array[1] and Array[0] Array[1] Array[2]
@jovan2226 what is the result of var_dump(count($qty))
result is int(3)
perhaps you dont remove $this->order->InsertItemData() this line. Only insert_batch is ok here
I had updated my model in the post, is there anything I need to change?
0

You could modify each of the codes like below.

Controller :

$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();
for ($i = 0; $i < count($qty); $i++) {
    if ($qty[$i]) {
        $orderItems[] = array (
            // 'order_id' =>  $orderHeaderId ,
            'product_id' =>  7 ,
            'item' => $itemName[$i] ,
            'qty' =>  $qty[$i] ,
            'weight' => $weight[$i] ,
            'unit_price' => $unitPrice[$i] ,
            'transportation_price' => 0 ,
            'item_type' =>  'GOLD' ,
            'gp' => $gp[$i],
            'discount' =>  $discount[$i],
            'amt_without_tax' => 10,
            'tax' => 20 ,
            'amt_incl_tax' => 30,
            'tax_code' => 40 ,
        );
    }
}
$this->order->InsertItemData($orderHeader,$orderItems);

Model :

 public function InsertItemData($orderHeader,$orderItems) {
    //take order_id from 'order' table //from InsertData(model) function
    $orderHeaderId = $this->InsertData($orderHeader); 

    if($orderItems) {
        // append order_id array into each order item
        foreach ($orderItems as $key => $item) {
            $orderItems[$key] = array('order_id' =>  $orderHeaderId) + $orderItems[$key];
        }

        // Inserting Order Item
        $this->db->insert_batch('order_items', $orderItems);
    }
}

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.