John Davidson

php - Unrest value in a 'foreach' loop


I have an order like this

product quantity inventory
1 5 50
1 6 50
7 2 150
1 6 50

I am trying to loop in each product and decrease the inventory

Total inventory for product 1 is 50

in loop

product 1 the inventory become 45
product 1 the inventory becomes 39
product 7 the inventory becomes 148
product 1 the inventory becomes 44

Hers is the issue in the last loop the inventory has been rest to 50 again.

here is my code

foreach($order->productId->inventory as $currentRequestedCount){
$currentRequested = $order->quantity * $order->relatedPackage ->unit_count;
if($currentRequestedCount->type == "existing"){
$currentRequested -= $currentRequestedCount->amount;

how to prevent the $currentRequestedCount from being reset?

My Answer:
PHP script refers to the level of dissatisfaction or unease in the codebase. This can be caused by various factors such as messy code, lack of documentation, inconsistent coding styles, or unresolved bugs. High unrest value indicates that the codebase may be difficult to maintain, debug, or extend in the future. It is important for developers to address and reduce unrest in their PHP scripts to ensure the codebase remains manageable and efficient.

Rate this post

3 of 5 based on 4149 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us