2010-11-08 130 views
0

嗨必須檢查2個國家,以設置運輸成本。 (這些值先前通過下拉列表分配)。這是我的PHP條件,它似乎不工作。如果我犯了一個錯誤,有人可以幫助我解釋語法嗎?PHP狀態檢查國家

<?php 
for($y = 0; $y < count($contentsArray); $y++) 
{ 

    $itemArray = explode(":", $contentsArray[ $y ]); 

    $price = number_format($itemArray[ 2 ] * $itemArray[ 1 ], 2); 
    $subtotal += $price; 

    echo "<tr>"; 
    echo "<td class='item_col'>" . $itemArray[ 0 ] . "</td>"; 
    echo "<td class='description_col'>" . $itemArray[ 3 ] . "</td>"; 
    echo "<td class='quantity_col'>" . $itemArray[ 1 ] . "</td>"; 
    echo "<td class='price_col'>$ " . $price . "</td>"; 
    echo "</tr>"; 
} 

// calculate the shipping 



if($subtotal > 500) 
    $shipping = 0.00; 

// ----這是我嘗試添加我的病情和錯誤------

else if ($country == "Canada") 
    $shipping = 10.00; 
else if ($country == "USA") 
    $shipping = 10.00; 

// ----在上面,我在那裏試圖添加我的條件------

else $ shipping = 15.00;

$shipping = number_format($shipping, 2); 




// calculate the tax and total for the cart 
$gst = number_format(($subtotal + $shipping) * 0.06, 2); 
$total = $subtotal + $shipping + $gst; 

// update the totals in the db 
$cartData->totals = $shipping . "|" . $gst . "|" . $total; 
$updateSuccess = updateCartTotals($merchant, $cartData); 
?> 

</tbody> 
</table> 
<br /> 

<div id="invoice_totals"> 
    <div id="totals"> 
    $ <?=number_format($subtotal, 2)?><br /> 
    $ <?=number_format($shipping, 2)?><br /> 
    $ <?=number_format($gst, 2)?><br /> 
    <span class="total">$ <?=number_format($total, 2)?></span> 
    </div> 

    <div id="headings"> 
    <strong>subtotal:</strong><br /> 
    <strong>shipping:</strong><br /> 
    <strong>tax:</strong><br /> 
    <span class="total">total:</span> 
    </div> 

    <div id="print"> 
    <a href="#"><img src="images/cart/buttons/print.gif" width="41" height="41" border="0" onmouseover="this.src='images/cart/buttons/print_ov.gif'" onmouseout="this.src='images/cart/buttons/print.gif'" /></a> 
    </div> 

    <div class="cleaner"></div> 

    <div id="back"> 
    <a href="shipping.html"><img src="images/cart/buttons/back.gif" width="58" height="58" border="0" onmouseover="this.src='images/cart/buttons/back_ov.gif'" onmouseout="this.src='images/cart/buttons/back.gif'" /></a> 
    </div> 
    <div class="cleaner"></div> 
</div> 

</div> 

<div id="shipping_information"> 
<p> 
    <strong>Shipping to:</strong><br /> 
    <?=$customerData->firstName . " " . $customerData->lastName?><br /> 
    <?=$customerData->address?><br /> 
    <?=$customerData->city . ", " . $customerData->province?><br /> 
    <?=getCountryName($customerData->country)?><br /> 
    <?=$customerData->postalCode?> 
</p> 

... 
+1

一切看起來都很好,你能解釋「似乎沒有工作」嗎?它應該做什麼,它實際上在做什麼,你認爲是錯誤的? – Tesserex 2010-11-08 22:41:43

+0

考慮到這種結構,只有在小計低於500時纔會檢查國家。當您嘗試此操作時,小計是多少? – 2010-11-08 22:46:20

+0

我沒有看到錯誤,假設這兩個國家也有資格免費送貨超過500個發明者。如果不是,並且您的測試滿足這種條件,那麼這就是您的問題,並且有條件的需要降到國家檢查以下。你可以給我們'var_dump($ country);'?的輸出嗎?你可能想使用'if(!stristr($ country,'canada'))'',但是如果我們有更多的信息會有幫助。 – Dereleased 2010-11-08 22:52:12

回答

0

爲了讓我們進一步幫助您,請在代碼中添加以下兩項補充(正是我在下面添加它們的位置)。運行它,然後將「DEBUG」消息粘貼到您的StackOverflow問題上。

<?php 
for($y = 0; $y < count($contentsArray); $y++) 
{ 

    $itemArray = explode(":", $contentsArray[ $y ]); 

    $price = number_format($itemArray[ 2 ] * $itemArray[ 1 ], 2); 
    $subtotal += $price; 

    echo "<tr>"; 
    echo "<td class='item_col'>" . $itemArray[ 0 ] . "</td>"; 
    echo "<td class='description_col'>" . $itemArray[ 3 ] . "</td>"; 
    echo "<td class='quantity_col'>" . $itemArray[ 1 ] . "</td>"; 
    echo "<td class='price_col'>$ " . $price . "</td>"; 
    echo "</tr>"; 
} 

// calculate the shipping 

// ADDITION #1 
echo "DEBUG: subtotal is $subtotal;<br>DEBUG: country is $country"; 
if($subtotal > 500) 
    $shipping = 0.00; 
// ---- This is where I'm trying to add my condition and bugs ------ 

else if ($country == "Canada") 
    $shipping = 10.00; 
else if ($country == "USA") 
    $shipping = 10.00; 
// ---- Above where I'm trying to add my condition ------ 

// ADDITION #2 
else $shipping = 15.00; 
echo "DEBUG: shipping is $shipping"; 
+0

這裏是調試輸出: DEBUG:小計12.95; DEBUG:country is DEBUG:shipping is 15 – detonate 2010-11-09 14:47:39

+0

這意味着這段代碼工作正常。您的錯誤發生在**'else $ shipping = 15.00;''行後面的某個時間**。在整個代碼中保留「$ shipping」的值,直到找到它的更改位置爲0。 – 2010-11-09 16:05:47

+0

是否有機會獲取「國家/地區」值? – detonate 2010-11-09 16:42:03