0

I have MySQL database used with Codigniter including following table too.

+-------------------+-----------------+------+-------+--------+
| update_request_id | update_stock_id | item | r_qty | status |
+-------------------+-----------------+------+-------+--------+

And used the following for loop in Controller, to update records in the table.

    $count = count($this->input->post('item_id'));
    $items = $this->input->post('item_id'); 
    $qts = $this->input->post('qty');                      
    
    for ($x = 0; $x < $count; $x++) {
        $udetails[$x]['update_stock_id'] = $id;
        $udetails[$x]['item'] = $items[$x];
        $udetails[$x]['r_qty'] = $qts[$x];              
        $udetails[$x]['status'] = 1;
    }

And the following View used to get relevant values for the controller.

<?php
if(!empty($itemReqFromHD)){
    $newMaster=$itemReqFromHD[0];
}
?>
<script type="text/javascript">

    $(document).on("change", "#item", function () {
       
        $.ajax({
            'url': '<?=site_url("item/isExistProduct/?q=")?>' + $('#item').val(),
            'method': 'GET',
            'success': function (data) {
                //console.log(data);

                var jData = JSON.parse(data);
                if (jData.status == true) {

                    $('#request_table').append('<tr>' +
                        '<td ><span id="product" >' + jData.data[0].item_name + '</span>' +
                        '<input type="hidden" id="item_id[]" name="item_id[]" value="'+jData.data[0].item_id+'">' +
                        '</td>' +
                        '<td class="text-center"><input class="form-control" autofocus required type="number" step="any" id="qty['+jData.data[0].item_id+']" name="qty['+jData.data[0].item_id+']" ></td>' +
                        '<td class="text-center" ><i class="fa fa-remove remove" style="cursor: pointer"></i></td>' +
                        '</tr>');
                }

            },
            'error': function () {

            }
        });


    });

    $(document).on("click", ".remove", function () {
        $(this).closest('tr').remove();
    });

</script>

<div class="box box-info">
    <div class="box box-info collapsed-box">
        <div class="box-header with-border">
            <h3 class="box-title">Update Item Request From HD</h3>

            <div class="box-tools pull-right">

                <button type="button" class="btn btn-box-tool" style="font-size: 16px;" data-widget="collapse"><i
                            class="fa fa-plus"></i>
                </button>

            </div>
            <!-- /.box-tools -->
        </div>
        <!-- /.box-header -->
        <div class="box-body" style="display: block">
            <form action="<?= site_url('item/editItemReqFromHD/'.$newMaster->update_stock_id) ?>" method="post">
                <div class="row">
                    
                    <div class="col-md-3">
                        <div class="form-group"><label>Request Date</label>
                            <input type="date" name="request_date" id="request_date" class="form-control" value="<?=$newMaster->billed_date?>">
                        </div>
                    </div>                   
                

                    <div class="col-md-6">
                        <div class="form-group"><label>Select Product</label>
                            <select name="item" id="item" class="form-control select2" >
                                <option value="">Select Product</option>
                                <?php
                                if (!empty($products)) {
                                    foreach ($products as $row) {

                                        ?>

                                        <option value="<?= $row->item_id ?>"><?= $row->item_name ?></option>
                                        <?php
                                    }
                                }

                                ?>
                            </select>

                        </div>
                    </div>

                </div>


                <!--table-->
                <div class="col-md-12 column">
                    <div class="col-md-12">
                        <div class="control-group table-group">
                            <label class="table-label">Request Items *</label>

                            <div class="controls table-controls">
                                <table id="request_table"
                                       class="table items table-striped table-bordered table-condensed table-hover">
                                    <thead>
                                    <tr style="background-color: #3c8dbc">
                                        <th class="col-md-8">Product Name</th>

                                        <th class="text-center" class="col-md-2">Quantity</th>

                                        </th>
                                        <th style="width: 30px !important; text-align: center;">
                                            <i class="fa fa-trash-o" style="opacity:0.5; filter:alpha(opacity=50);"></i>
                                        </th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php
                                        foreach ($itemReqFromHD as $row){

                                    ?>
                                    <tr>
                                        <td ><?=$row->item_name?>
                                            <input type="hidden" id="item_id[]" name="item_id[]" value="<?=$row->item_id?>">
                                        </td>
                                        <td class="text-center"><input class="form-control" value="<?=$row->r_qty?>" required type="number" step="any" id="r_qty[<?=$row->item_id?>]" name="r_qty[<?=$row->item_id?>]" ></td>
                                        <td class="text-center"><i class="fa fa-remove remove" style="cursor: pointer"></i></td>
                                    </tr>
                                    <?php
                                    }
                                    ?>
                                    </tbody>

                                </table>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-8"></div>
                    <div class="col-md-4">
                        <button type="submit" class="btn btn-primary btn-block">Update Request</button>
                    </div>
                </div>
            </form>   
           
        </div>

    </div>
</div>

All codes are working fine but at the time of the update the column 'r_qty' is not edited, leaving the table as follows:

Array
(
    [0] => Array
        (
            [update_stock_id] => 577
            [item] => 2
            [r_qty] => 
            [status] => 1
        )

    [1] => Array
        (
            [update_stock_id] => 577
            [item] => 4
            [r_qty] => 
            [status] => 1
        )

)    

I could not catch what I am going wrong ? Can anyone help me ?

Tom Lima
  • 1,027
  • 1
  • 8
  • 25
mcode
  • 87
  • 10
  • can you print the value of $ qts [$ x]? It would also be interesting to post the code that makes the update, it seems to me that this is just the loop, correct? – Tom Lima Jul 07 '20 at 15:01
  • @ Tom Lima. $ qts [$ x] returns empty – mcode Jul 07 '20 at 15:05

0 Answers0