2013-10-30 58 views
0

我在一個循環的混亂碰到,其實,我想檢查一下,

第一個條件:如果憑證類型是AMOUNT = AMOUNTvalue > 0 and $totalRules <= 1表示這是第一次如果用戶正在使用coupson,則讓用戶使用它。

第二個條件:我在檢查是否AMOUNT = AMOUNTvalue > 0 and $totalRules >=1表示用戶已經使用了優惠券。如何檢查是否優惠券已被使用

THRID條件:如果AMOUNT !=AMOUNT$totalRules >=1意味着這段時間,用戶選擇不同的優惠券,這可能是百分比類型,那麼我們是不是允許用戶使用此優惠券。

現在,對於第一次,代碼工作正常,但如果刷新頁面,即使我用break;

任何人都可以請幫我解決這問題,其他條件也越來越真的嗎?我知道我需要改進這些代碼,我需要老年人的幫助。

$vDetail = array('AMOUNT', 'ONLY'); // For now check if the coupon type is AMOUNT (set from admin) 
    $voucherAllowed = 1; // Total number of voucher allowed, Global variable. 
    $totalRules = 1; // First tiem it'll be 0, then always adds 1 in that variable to check if user has already used any coupon. 
    $voucherTypeArray ['AMOUNT'] = 8.00; 
    $voucherTypeArray ['PERCENT'] = 0.00; 



foreach($voucherTypeArray as $thisVoucher => $vValue) 
{ 
    echo "$vDetail[0] == $thisVoucher && $vValue>0 && $totalRules <= $voucherAllowed <br>"; 
    if($vDetail[0]==$thisVoucher && $vValue>0 && (int)$totalRules <= $voucherAllowed) 
    { $throwError = '0'; break; } // Continue to the next rule and add this one. 
    elseif($vDetail[0]==$thisVoucher && $vValue > 0 && (int)$totalRules >= $voucherAllowed) 
    { 
     $throwError = "Only $voucherAllowed voucher can be redeem at this time."; 
     break; 
    } 
    elseif($vDetail[0]!=$thisVoucher && (int)$totalRules >= $voucherAllowed) 
    { 
     $throwError = 'aYou cannot redeem this voucher at this time.'; 
     break; 
    } 
    } 
+0

你的第一和第二個條件都可以是'在同一時間TRUE',如果'$ totalRules == 1'。 – AlexP

+0

@AlexP所以你認爲,我不應該使用比較運算符<= – Nadeem

+0

「我需要老年人」是我的一天。 –

回答

1

兩個問題我與你的代碼中看到:

if($vDetail[0]==$thisVoucher && $vValue>0 && (int)$totalRules <= $voucherAllowed) 

$vDetail不是數組但你訪問它本身。

這意味着它將把$vDetail視爲一個字符數組(在這種情況下取​​第一個字母A)。確保這是你的意思(你的代碼有效比較:$voucher['AMOUNT'] == 'A')。

而且你的條件語句不考慮$totalRules == 1

+0

'$ vDetail' **是一個數組,請再看看$ vDetail [0] =='AMOUNT'' – AlexP

+0

@AlexP no '$ voucher ['AMOUNT'] = 8;''和'$ voucher ['PERCENT'] = 0.0' 它在數組中 PHP-FIDDLE:http://phpfiddle.org/main/code/3g2-4vj – Nadeem

+1

@AlexP:這個問題在我的答案後編輯成一個數組。請檢查編輯歷史記錄。如果這是您低估的原因,請撤消它。謝謝! – webbiedave