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

}

jovan2226
  • 17
  • 5
  • Can you post your code on your controller? – Roshan Feb 26 '19 at 01: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 https://stackoverflow.com/questions/3314567/how-to-get-form-input-array-into-php-array – Eiji Feb 26 '19 at 01:27
  • @Roshan I had updated my controller – jovan2226 Feb 26 '19 at 01:43
  • @Eiji Thanks, I will take a look for this solution ! – jovan2226 Feb 26 '19 at 01:44

2 Answers2

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);
}
Rejoanul Alam
  • 5,435
  • 3
  • 39
  • 68
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);
    }
}
Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26