开发者

Why does not $grandtotal show correctly?

Let's say $_SESSION['totalprice'] is 1200. However echo $totalprice; outputs 1200 and echo $grandtotal; outputs 66. Grandtotal should be 1265.

What am I doing wrong here?

$totalprice = $_SESSION['totalprice'];
$shipping= 65;

if (count($_SESSION['cart'])){
 $count = 1;
 foreach ($_SESSION['cart'] as $PID => $row){ 
  echo "<p class='padnmgn'><b>". $row['count'] . " " . $row['name'] . " @ " . $row['price']."</b></p><br/>\n";
  echo "<input type='hidden' name='item_name_".$count."' value='".$row['name']."'/>\n";
  echo "<input type='hidden' name='item_quantity_".$count."' value='".$row['count']."'/>\n";
  echo "<input type='hidden' name='item_price_".$count."' value='".$row['price']."'/>\开发者_运维百科n";
  echo "<input type='hidden' name='item_currency_".$count."' value='NOK'/>\n";
  echo "<input type='hidden' name='ship_method_name_".$count."' value='Posten'/>\n";
  echo "<input type='hidden' name='ship_method_price_".$count."' value='65.00'/>\n";

 }
}
$grandtotal = $totalprice + $shipping;

echo $totalprice;
echo $grandtotal;


Try this:

$grandtotal = ((int) $totalprice) + $shipping;


Are you sure the value inside $totalprice is actually an integer and are you sure it's not being modified before you get to the addition? Try doing var_dump($totalprice); just before you do the addition, to see what value it really has at that point.

If it does have the value you expect, then you may need to cast it to an integer explicitly for the calculation to work properly, so something like (int)$totalprice + $shipping; etc.

For example,

<?php
    $totalprice = "1200blahblah";
    $shipping = 65;
    $grandtotal = (int)$totalprice + $shipping;
    echo $grandtotal; // still prints "1265"
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜