Matching and recording data from multiple dynamic arrays php [closed] - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Saturday, April 1, 2023

Matching and recording data from multiple dynamic arrays php [closed]

In my code below, I have more than one product and parcel dynamically. Each product and each parcel has its own ID values. What I want to do is to match the product quantities with the relevant product and parcel ID number and save them to the database.

    <div class="card card-default card-md mt-3 mb-4">
            <div class="card-header">
                <h6>Parcel Information</h6>
            </div>
            <div class="card-body">
                <div class="form-group row">
                    <table class="table mb-1 table-borderless adv-table mt-0" data-sorting="true" data-filter-container="#filter-form-container" data-paging-current="1" data-paging-position="right" data-paging-size="10">
                        <thead>
                            <tr class="userDatatable-header">
                                <th>
                                    <span class="userDatatable-title">
                                        Parcel Number
                                    </span>
                                </th>
                                <th>
                                    <span class="userDatatable-title">
                                        Parcel Length
                                    </span>
                                </th>
                                <th>
                                    <span class="userDatatable-title">
                                        Parcel Width
                                    </span>
                                </th>
                                <th>
                                    <span class="userDatatable-title">
                                        Parcel Height
                                    </span>
                                </th>
                                <th>
                                    <span class="userDatatable-title">
                                        Parcel Weight
                                    </span>
                                </th>
                            </tr>
                        </thead>
                        <tbody id="dynamic_field">
                            <tr>
                                <td>
                                    <div class="userDatatable-content">
                                        <div class="form-group">
                                            <input type="text" class="form-control ih-medium ip-lightradius-xs b-light parcelID" name="parcelNumber[]" disabled value="#1">
                                        </div>
                                    </div>
                                </td>
                                <td id="dynamic_field">
                                    <div class="userDatatable-content">
                                        <div class="form-group">
                                            <input type="text" class="form-control ih-medium ip-lightradius-xs b-light" name="parcelLenght[]">
                                        </div>
                                    </div>
                                </td>
                                <td id="dynamic_field">
                                    <div class="userDatatable-content">
                                        <div class="form-group">
                                            <input type="text" class="form-control ih-medium ip-lightradius-xs b-light" name="parcelWidth[]">
                                        </div>
                                    </div>
                                </td>
                                <td id="dynamic_field">
                                    <div class="userDatatable-content">
                                        <div class="form-group">
                                            <input type="text" class="form-control ih-medium ip-lightradius-xs b-light" name="parcelHeight[]">
                                        </div>
                                    </div>
                                </td>
                                <td id="dynamic_field">
                                    <div class="userDatatable-content">
                                        <div class="form-group">
                                            <input type="text" class="form-control ih-medium ip-lightradius-xs b-light" name="parcelWeight[]">
                                        </div>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div class="userDatatable-content">
                                        <div class="form-group">
                                            <input type="text" class="form-control ih-medium ip-lightradius-xs b-light parcelID" name="parcelNumber[]" disabled value="#2">
                                        </div>
                                    </div>
                                </td>
                                <td id="dynamic_field">
                                    <div class="userDatatable-content">
                                        <div class="form-group">
                                            <input type="text" class="form-control ih-medium ip-lightradius-xs b-light" name="parcelLenght[]">
                                        </div>
                                    </div>
                                </td>
                                <td id="dynamic_field">
                                    <div class="userDatatable-content">
                                        <div class="form-group">
                                            <input type="text" class="form-control ih-medium ip-lightradius-xs b-light" name="parcelWidth[]">
                                        </div>
                                    </div>
                                </td>
                                <td id="dynamic_field">
                                    <div class="userDatatable-content">
                                        <div class="form-group">
                                            <input type="text" class="form-control ih-medium ip-lightradius-xs b-light" name="parcelHeight[]">
                                        </div>
                                    </div>
                                </td>
                                <td id="dynamic_field">
                                    <div class="userDatatable-content">
                                        <div class="form-group">
                                            <input type="text" class="form-control ih-medium ip-lightradius-xs b-light" name="parcelWeight[]">
                                        </div>
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div id="productInfo">
            <div class="card card-default card-md mt-3 mb-4">
                <div class="card-header">
                    <h6>Product Information</h6>
                </div>
                <div class="card-body">
                    <div class="form-group row" style="overflow-x: scroll">
                        <table class="table mb-1 table-borderless adv-table mt-0" data-sorting="true" data-filter-container="#filter-form-container" data-paging-current="1" data-paging-position="right" data-paging-size="10">
                            <thead>
                            <tr class="userDatatable-header">
                                <th>
                                    <span class="userDatatable-title">
                                        Product ASIN Number
                                    </span>
                                </th>
                                <th>
                                    <span class="userDatatable-title">
                                        #1
                                    </span>
                                </th>
                                <th>
                                    <span class="userDatatable-title">
                                        #2
                                    </span>
                                </th>
                            </tr>
                            </thead>
                            <tbody>

                            <!-- 
                            foreach ($_POST["id"] as $id) {

                                $product_query  =   $db->prepare("SELECT id, productName, productASINNumber FROM products WHERE id = ? AND deleteStatus = ?");
                                $product_query->execute([$id, 0]);
                                $product    =   $product_query->fetch(PDO::FETCH_ASSOC);

                               -->
                                <input type="hidden" class="form-control ih-medium ip-lightradius-xs b-light" name="product[]" value="<?php echo $id ?>">
                                <tr>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="text" class="form-control ih-medium ip-lightradius-xs b-light"  disabled value="D4B5X1Z3AS5">
                                            </div>
                                        </div>
                                    </td><td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="number" min="1" max="100" id="productQTY" name="productQTY[]" class="form-control ih-medium ip-lightradius-xs b-light" placeholder="Quantity">
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="number" min="1" max="100" id="productQTY" name="productQTY[]" class="form-control ih-medium ip-lightradius-xs b-light" placeholder="Quantity">
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="text" class="form-control ih-medium ip-lightradius-xs b-light"  disabled value="S5X1Z35S1Z">
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="number" min="1" max="100" id="productQTY" name="productQTY[]" class="form-control ih-medium ip-lightradius-xs b-light" placeholder="Quantity">
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="number" min="1" max="100" id="productQTY" name="productQTY[]" class="form-control ih-medium ip-lightradius-xs b-light" placeholder="Quantity">
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                 <tr>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="text" class="form-control ih-medium ip-lightradius-xs b-light"  disabled value="S5X1Z35S1Z">
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="number" min="1" max="100" id="productQTY" name="productQTY[]" class="form-control ih-medium ip-lightradius-xs b-light" placeholder="Quantity">
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="number" min="1" max="100" id="productQTY" name="productQTY[]" class="form-control ih-medium ip-lightradius-xs b-light" placeholder="Quantity">
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                 <tr>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="text" class="form-control ih-medium ip-lightradius-xs b-light"  disabled value="S5X1Z35S1Z">
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="number" min="1" max="100" id="productQTY" name="productQTY[]" class="form-control ih-medium ip-lightradius-xs b-light" placeholder="Quantity">
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="number" min="1" max="100" id="productQTY" name="productQTY[]" class="form-control ih-medium ip-lightradius-xs b-light" placeholder="Quantity">
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                 <tr>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="text" class="form-control ih-medium ip-lightradius-xs b-light"  disabled value="S5X1Z35S1Z">
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="number" min="1" max="100" id="productQTY" name="productQTY[]" class="form-control ih-medium ip-lightradius-xs b-light" placeholder="Quantity">
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="number" min="1" max="100" id="productQTY" name="productQTY[]" class="form-control ih-medium ip-lightradius-xs b-light" placeholder="Quantity">
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                 <tr>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="text" class="form-control ih-medium ip-lightradius-xs b-light"  disabled value="S5X1Z35S1Z">
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="number" min="1" max="100" id="productQTY" name="productQTY[]" class="form-control ih-medium ip-lightradius-xs b-light" placeholder="Quantity">
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="userDatatable-content">
                                            <div class="form-group">
                                                <input type="number" min="1" max="100" id="productQTY" name="productQTY[]" class="form-control ih-medium ip-lightradius-xs b-light" placeholder="Quantity">
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            <!-- <?php } ?> -->
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

The picture below belongs to the code above. Parcel quantities are dynamic. It can be reproduced with the help of a button. So I need a dynamic structure.

ID information of products = product[]

Quantities of Products = productQTY[]

Parcels ID = parcelNumber[]

I get the above data to my php file with ajax;

AJAX:

$('#shipment-add').on('submit', function (){
    type = 2;
    var shipmentName        =   $('input[name=shipmentName]').val();
    var shipmentID          =   $('input[name=shipmentID]').val();
    var referanceID         =   $('input[name=referanceID]').val();
    var supplierID          =   $('select[name=supplierID]').val();
    var warehouseID         =   $('select[name=warehouseID]').val();
    var parcelNumber        =   $("input[name='parcelNumber[]']").map(function(){return $(this).val();}).get();
    var parcelLenght        =   $("input[name='parcelLenght[]']").map(function(){return $(this).val();}).get();
    var parcelWidth         =   $("input[name='parcelWidth[]']").map(function(){return $(this).val();}).get();
    var parcelHeight        =   $("input[name='parcelHeight[]']").map(function(){return $(this).val();}).get();
    var parcelWeight        =   $("input[name='parcelWeight[]']").map(function(){return $(this).val();}).get();
    var product             =   $("input[name='product[]']").map(function(){return $(this).val();}).get();
    var productQTY          =   $("input[name='productQTY[]']").map(function(){return $(this).val();}).get();
    var id          =   $("input[name='productQTY[]']").map(function(){return $(this).data("id");}).get();

    $.ajax({
        url: '../request/shipment/shipment.php',
        type: 'POST',
        data: {
            'shipmentName': shipmentName,
            'shipmentID': shipmentID,
            'referanceID': referanceID,
            'supplierID': supplierID,
            'warehouseID': warehouseID,
            'parcelNumber': parcelNumber,
            'parcelLenght': parcelLenght,
            'parcelWidth': parcelWidth,
            'parcelHeight': parcelHeight,
            'parcelWeight': parcelWeight,
            'product': product,
            'productQTY': productQTY,
            'type': type,
            'id': id,
        },
        success: function (val){
            console.log(val);
        }
    })
});

PHP:

            $shipmentName       =   $_POST["shipmentName"];
    $shipmentID         =   $_POST["shipmentID"];
    $referanceID        =   $_POST["referanceID"];
    $supplierID         =   $_POST["supplierID"];
    $warehouseID        =   $_POST["warehouseID"];
    $parcelNumber       =   $_POST["parcelNumber"];
    $parcelLenght       =   $_POST["parcelLenght"];
    $parcelWidth        =   $_POST["parcelWidth"];
    $parcelHeight       =   $_POST["parcelHeight"];
    $parcelWeight       =   $_POST["parcelWeight"];
    $product            =   $_POST["product"];
    $productQTY            =   $_POST["productQTY"];

    $parcelNumberClear  =   [];
    foreach ($parcelNumber as $parcels){
        $s = ltrim($parcels, "#");
        array_push($parcelNumberClear, $s);
    }

    $new  =    array("parcel"=> $parcelNumberClear, "product"=>$product, "qty" =>$productQTY);
    foreach ($parcelNumberClear as $value) {
        $data["parcel"] =    $value;
    }
    foreach ($product as $value) {
        $data["product"]    =   $value;
    }
    foreach ($productQTY as $value) {
        $data["qty"]    =   $value;
    }
    $all[]    =   $data;

    foreach ($productQTY as $value) {
        print_r($parcelNumberClear);
    }

enter image description here

For example, the input value of my product with ID number 3 in the parcel with ID number 2 is "6", I want to tell the database that the value of the product with ID number 3 and the parcel with ID number 2 = 6.

I tried many ways but unfortunately I couldn't achieve my goal. Can you help me?

By the way, please forgive me for opening the wrong question.



source https://stackoverflow.com/questions/75893337/matching-and-recording-data-from-multiple-dynamic-arrays-php

No comments:

Post a Comment