Am trying to put together a method that looks through a Codeigniter shopping cart and determines the type of transaction based on the id of the items in the cart
Heres the method
function parse_transaction_type() {
$card_skus = array("MYU_SC1","MYU_SC2","MYU_SC3");
$fee_skus = array("MYU_SF1","MYU_AD1","MYU_AD2");
foreach ($this->cart->contents() as $key => $item) {
if(in_array($item['id'], $card_skus) && in_array($item['id'], $fee_skus))
{
$type = "fees-cards";
}
if (in_array($item['id'], $card_skus) && !in_array($item['id'], $fee_skus))
{
$type ="cards";
}
if (in_array($item['id'], $fee_skus) && !in_array($item['id'], $card_skus))
{
$type ="fees";
}
}
echo $type;
}
The method only returns either “cards” or “fees” even when both are present. What am doing wrong?
You should use boolean flags for each type you are checking for, then assign the string (or do whatever) based on the value of the two flags. Initialize them to FALSE before your loop, then when you discover an item of each type, set it’s flag to TRUE.
You can then optimize the loop to check if both are already true and
break, so that in the case where the first two items are one of each, you can skip the rest.