0

im trying to get the total quantity

    The data from array would look like this;
    [desc]  [quantity]
    [foo1], [   2    ]
    [foo2], [   2    ]
....

This is the "code":

    function saveInvoiceDetail(array $wreceipt_items, $warehouse = '', $tracking = ''){
    global $con;

    $y = 0;

    foreach ($wreceipt_items as $wreceipt_item){
        $desc = mysqli_real_escape_string( $con, trim( $wreceipt_item['desc'] ) );
        $quantity = mysqli_real_escape_string( $con, trim( $wreceipt_item['quantity'] ) );

        $query = "INSERT INTO wreceipt_items (`id`, `warehouse`, `desc`, `quantity`)
                VALUES (NULL, '$warehouse', '$desc', '$quantity')";
    $y++;
        mysqli_query($con, $query);
    }

$quantity_sum = array_sum($wreceipt_item['quantity'])

    $x = $quantity_sum; //repeat this insert ($quantity_sum) times
    for ($i = 1; $i <= $x; $i++){
            $query2 = "INSERT INTO control (`id`, `warehouse`, `tracking`)
                VALUES ('', '$warehouse-$i', '$tracking')";
            mysqli_query($con, $query2);
    }

}

Desired result: execute '$query2' X times, where X = $quantity_sum; Can this be possible? I cant figure it out.

Note: the 'foreach' & 'for' works but i cant use the 'for' without the 'quantity sum'

EDIT - FIXED

function saveInvoiceDetail(array $wreceipt_items, $warehouse = '', $tracking = ''){
    global $con;

    $y = 0;

    foreach ($wreceipt_items as $wreceipt_item){
        $desc = mysqli_real_escape_string( $con, trim( $wreceipt_item['desc'] ) );
        $quantity = mysqli_real_escape_string( $con, trim( $wreceipt_item['quantity'] ) );

        $query = "INSERT INTO wreceipt_items (`id`, `warehouse`, `desc`, `quantity`)
                VALUES (NULL, '$warehouse', '$desc', '$quantity')";
    $y++;
        mysqli_query($con, $query);
    }

$quantity_sum = array_sum(array_column($wreceipt_items, 'quantity'));

    $x = $quantity_sum; //repeat this insert ($quantity_sum) times
    for ($i = 1; $i <= $x; $i++){
            $query2 = "INSERT INTO control (`id`, `warehouse`, `tracking`)
                VALUES ('', '$warehouse-$i', '$tracking')";
            mysqli_query($con, $query2);
    }

}
2
  • What's the question? You've stated the desired result and then stated that your code already works. Why would you want to "user the 'for' without the quantity sum" if your stated goal is to execute $query2 a number of times equal to quantity sum? That is illogical! Commented Feb 7, 2019 at 21:47
  • I want to sum the value of quantity from array to execute the 'for' statement. Because i need to insert info into table that number of times. (Query2) Commented Feb 7, 2019 at 23:23

1 Answer 1

1

One solution would be to use array_column to extract all the quantity values from $wreceipt_items and sum them:

$quantity_sum = array_sum(array_column($wreceipt_items, 'quantity'));
Sign up to request clarification or add additional context in comments.

1 Comment

@IrvıngNgr' Did this answer your question? If not, could you provide more information to help answer it?

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.