Welcome Guest, Not a member yet? Register   Sign In
Wrong output using foreach array loop
#1

[eluser]rochellecanale14[/eluser]
I just need a little help here about looping a foreach array. I created a form that has a selection box that sets the mode to INVALID and INVALID. In displaying the correct mode using selection box. I don't have a problem with that but after passing the values to my form. I don't get the correct value. I can't spot where's my error. Please help me guys. Here's my code below.

Here's the loop part:

Code:
$val = 0;

foreach($order_info_list->result_array() as $details){

   $val++;

   echo "<tr>";
       echo "<td>&lt;input type='text' name='itemdesc[]' value = ".$details['item_desc']." class='k-textbox' /&gt;&lt;/td>";
       echo "<td>&lt;input type='text' name='qty[]' value=".$details['item_qty']." id='qty{$val}' class='k-textbox' /&gt;&lt;/td>";
       echo "<td>&lt;input type='text' name='price[]' value=".number_format($details['item_price'],2)." id='price$val' class='k-textbox' /&gt;&lt;/td>";
       echo "<td>&lt;input type='text' name='total[]' class='k-textbox' id='total{$val}'  courier; text-align: right; background-color: lightgray; color: red' read value='".$details['total']."' /&gt;&lt;/td>";

       echo "<td width='30%'  center; font-size: 12px'>";

       $stat = $details['status'];

       echo "<select name='status[]' id='status_validate$val' class='status_select'>";    
       //if($stat == 'VALID'){
           echo "<option value='VALID'>VALID</option>";
           echo "<option value='INVALID'>INVALID</option>";
        //}else{
        // echo "<option value='VALID'>VALID</option>";
         //echo "<option value='INVALID' selected='selected'>INVALID</option>";
        //}
       echo "</select>";

       echo "</td>";
      echo "</tr>";

     $idx = $details['poid'];

     echo "&lt;input type='hidden' name='idx[]' value='{$idx}' /&gt;";

     $code_x = $details['order_code'];

  }

The JS part (it is working)

Code:
$('[id^=status_validate]').on('change',function(){

       var index = this.id.match(/\d+/)[0];

       var status_set = $("#status_validate"+index).val();
       var price_set = $("#price"+index).val();
       var qty_set = $("#qty"+index).val();
       var total_set = $("#total"+index).val();

       var temp_price = price_set;
       var temp_qty = qty_set;
       var temp_total = total_set;
       var _gtotal = parseInt($('#gtotal').val());

       if(status_set == 'VALID'){

            restoreLast($("#price" + index));
            restoreLast($("#qty" + index));
            restoreLast($("#total" + index));

            var temp_gtotal = _gtotal + parseFloat($("#total"+index).val());

            $('#gtotal').val(temp_gtotal);

       }else{
            $("#price"+index).attr('data-last',price_set);
            $("#qty"+index).attr('data-last',qty_set);
            $("#total"+index).attr('data-last',total_set);

            var price_set = 0.00;
            var qty_set = 0.00;
            var total_set = 0.00;
            var temp_gtotal = _gtotal - parseFloat($("#total"+index).val());

            $("#price"+index).val(price_set);
            $("#qty"+index).val(qty_set);
            $("#total"+index).val(total_set);              
            $('#gtotal').val(temp_gtotal);
       }
    });

    function restoreLast(e){
        var x = 1;
        console.log(x);
        e.val(e.data('last'));
    }

Here's my model part:
Code:
public function updateItemOrder(){

            $id = $this->input->post('idx');
            $desc = $this->input->post('itemdesc');
            $qty = $this->input->post('qty');
            $price = $this->input->post('price');
            $status = $this->input->post('status');
            
            $code = $this->input->post('codes');
            
            $updateArray = array();
            
            
            
            for($x = 0; $x < sizeof($id); $x++){

                $total[] = $price[$x] * $qty[$x];
                
                $idvar = $desc[$x];
                
                $updateArray[] = array(
                    'poid' => $id[$x],
                    'item_desc' => $desc[$x],
                    'item_qty' => $qty[$x],
                    'price' => $price[$x],
                    'total' => $total[$x],
                    'status' => $status[$x]
                );
                
                $total_list[] = $total[$x];
                
            }
            echo "<pre>";
            print_r($updateArray);
            exit;

Here the sample output using form. If i have this form for example.

Code:
row1 = VALID
row2 = INVALID   //original is VALID, but what if the user sets the selectio to INVALID?
row3 = VALID
row4 = VALID
row5 = VALID
row6 = VALID
row7 = VALID
row8 = VALID
row9 = VALID
row10 = VALID
row11 = VALID

And in my model if i used print_r($status) it returns to:

[0] => VALID                     // row1
[1] => VALID                     // row1
[2] => INVALID                   // row2
[3] => INVALID                   // row2
[4] => VALID                     // row3
[5] => VALID                     // row3
[6] => VALID                     // row4
[7] => VALID                     // row4
[8] => VALID                     // row5
[9] => VALID                     // row5
[10] => VALID                    // row6
[11] => VALID                    // row6
[12] => VALID                    // row7
[13] => VALID                    // row7
[14] => VALID                    // row8
[15] => VALID                    // row8
[16] => VALID                    // row9
[17] => VALID                    // row9
[18] => VALID                    // row10
[19] => VALID                    // row10
[20] => VALID                    // row11
[21] => VALID                    // row11
So it means the value is doubled when passing to the array.

But it my table form i have only 11 rows.
Please help me guys with this. I can't spot where did I go wrong with my code. Thanks.
But it my table form i have only 11 rows.




Theme © iAndrew 2016 - Forum software by © MyBB