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.