PHP状态检查国家

时间:2010-11-08 22:38:55

标签: php

您必须检查2个国家/地区才能设置运费。 (这些值以前是通过下拉列表分配的)。这是我的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;

// ----在我试图添加条件的地方------

否则       $ 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 个答案:

答案 0 :(得分:0)

为了让我们进一步为您提供帮助,请在您的代码中添加以下两个新增内容(我在下面添加它们的确切位置)。运行它,然后在StackOverflow问题上粘贴“DEBUG”消息。

<?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";