PHP foreach auto increment if rules fulfilled

  mysql, php
    Table (Setting)
    runningnumber(AI)   food_sku    food_id    current_running_number
    1                    A100          130          A100-00300
    
    
    Table (inventory)
    inventory_id         food_id         running_number       stocks
    1                      130               A100-00300          0  
    2                      130               A100-00301          1 
    3                      130               A100-00302          1 
    4                      130               A100-00303          0 
    5                      130               A100-00304          1 
    6                      130               A100-00305          1 

for ($x = 1; $x <= $cart_item_total; $x++) {
    $get_food_running_number= "SELECT * FROM setting WHERE food_id= '" . $food_id. "' ";
    
    $check_stock ="SELECT * FROM inventory WHERE running_number = '" . $get_food_running_number['current_running_number'] "'";
    if($check_stock['stock'] <= 0){
        $error = 'Out of Stock';
    }
    $foodsku[$x] = ++$get_food_running_number['current_running_number']; 

}

Will check on the food stock based on the running number in inventory table , and it will echo error and once detect $error the whole operation will stop.

so now the problem that i met is if the item is out of stock , it will take the next number

let say cart_item_total is 3 item , so when it loop

A100-00301 , A100-00302, A100-00303 < but A100-00301 is out of stock ,
it will take A100-00304 instead of show error

What i tried , include $foodsku[$x] = ++$get_food_running_number['current_running_number']; in $check_stock query instead of error , but still unable to get what i want

Source: Ask PHP

LEAVE A COMMENT