I have a shop checkout function something like this:
$min_delivery = 25;
foreach ($shoppingCartItem as $item) {
$insert->name = $item->name;
$insert->price = $item->price;
$insert->quantity = $item->qty;
$insert->save();
$total += $item->price*$item->qty;
}
Is there any php function that would allow the foreach loop to happen only if ($total > $min_delivery) .
Or the only way would be to do the foreach twice, once only to calculate $total, then if ($total > $min_delivery) do a second foreach to insert into the database.
*EDIT - some details on why I want some other way instead of two loops:
The issue is that I can't trust the $item->price from the shopping cart because it comes from the user (and I don't verify it until checkout) so I need to check it against database before inserting.
So doing the loop twice would mean to query the database twice.
array_reduceto find the$total$totalis calculated in the loop, I'm not sure that only executing the loop based on its value is going to get you very far. If there's a class involved that wraps the whole cart, adding agetTotal()method to it would make this a lot easier, and avoid the need to have a second dedicated loop.