dongmei9020 2016-09-28 10:09
浏览 96
已采纳

PHP总和达到1000时是错误的

I have some problems with my shopping cart, it does not sum all the items when I add them, and the value reaches around 1000/1400, the sum is wrong and it just stops summing the items. Check this picture: http://prntscr.com/cn6uvg

There you can see 2 items with a value of 1000, and it should give 2000, but it instead gives 1001, and it only occurs when the sum is more than around 1000.

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
session_start();
include("admin/php/connect_to_mysql.php");
include("admin/php/myFunctions.php");
if(!empty($_GET['prodid'])){
    $pid = $_GET['prodid'];
    $wasFound = false;
    $i = 0;
    if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
        $_SESSION["cart_array"]=array(0=>array("productID"=>$pid,"quantity"=>1));
    }else{
        foreach($_SESSION["cart_array"] as $each_product){
            $i++;
            while(list($key,$value)=each($each_product)){
                if($key=="productID" && $value==$pid){  
                    array_splice($_SESSION["cart_array"],$i-1,1,array(array("productID"=>$pid,"quantity"=>$each_product ['quantity']+1)));
                    $wasFound=true;
                }
            }       
        }
        if($wasFound==false){
            array_push($_SESSION["cart_array"],array("productID"=>$pid,"quantity"=>1));
        }
    }
    header("location:shoppingcart.php");
    exit();
}
//-------------------------------------------------------------------------------------------------
$submit = $_POST['btnUpdate'];
if($submit == "Update"){
    $x = 0;
    $i = 0;
    //echo $_POST['txtQuan2'];
    //echo $_POST['txtHoldProdId0'];
    foreach($_SESSION["cart_array"] as $each_product){
        $i++;
        $quantity = $_POST['txtQuan'.$x];
        $prodStock = $_POST['txtHoldQuan'.$x];
        $prodAdjustId = $_POST['txtHoldProdId'.$x++];
        if($quantity<1){ $quantity = 1; }
        if($quantity>$prodStock){ $quantity = $prodStock; }
        while(list($key,$value)=each($each_product)){
            array_splice($_SESSION["cart_array"],$i-1,1,array(array("productID"=>$prodAdjustId,"quantity"=>$quantity)));
        }       
    }

}
//-------------------------------------------------------------------------------------------------
if(!empty($_GET['cid']) || isset($_GET['cid'])){
    $removeKey = $_GET['cid'];
    if(count($_SESSION["cart_array"])<=1){
        unset($_SESSION["cart_array"]);
    }else{
        unset($_SESSION["cart_array"]["$removeKey"]);
        sort($_SESSION["cart_array"]);
    }
}
//-------------------------------------------------------------------------------------------------
$cartTitle = "";
$cartOutput = "";
$cartTotal = "";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
    $cartOutput="<h2 align='center'> Your shopping cart is empty </h2>";
}else{
    $x = 0;
    $cartTitle .= '<form name="shoppingcart_form" action="shoppingcart.php" method="post" /><table width="700px" cellspacing="0" cellpadding="5">
            <tr bgcolor="#CCCCCC">
                        <th width="220" align="left">Image </th> 
                        <th width="140" align="left">Name </th> 
                        <th width="100" align="center">Quantity </th> 
                        <th width="60" align="center">Stock </th> 
                        <th width="60" align="right">Price </th> 
                        <th width="60" align="right">Total </th> 
                        <th width="90"> </th></tr>';
    $i = 0;
    foreach($_SESSION["cart_array"] as $each_product){
        $product_id = $each_product['productID'];
        $sql=mysql_query("select * from tblproduct where prod_id='$product_id' limit 1");
        while($row=mysql_fetch_array($sql)){
            $prodNo = $row["prod_no"];
            $prodID = $row["prod_id"];
            $prodName = $row["prod_name"];
            $prodPrice = $row["prod_price"];
            $prodQuan = $row["prod_quan"];
        }
        $pricetotal=$prodPrice*$each_product['quantity'];
        $cartTotal= number_format($pricetotal+$cartTotal,2);
        $cartOutput .= '<tr><td><img style="border: 2px solid;" src="images/product/'.$prodNo.'.jpg" width="150" height="120" /></td> 
            <td>'.$prodName.'</td> 
            <td align="center"><input type="hidden" name="txtHoldProdId'.$i.'" value="'.$prodID.'" /><input name="txtQuan'.$i.'" type="text" value="'.$each_product['quantity'].'" style="width: 40px; text-align: center" /> </td>
            <td align="center"><input type="hidden" name="txtHoldQuan'.$i.'" value="'.$prodQuan.'" /> <span style="color:#FF0000;">*Note</span></td> 
            <td align="right">$'.$prodPrice.'</td> 
            <td align="right">$'.$pricetotal.'</td>
            <td align="center"> <a href="shoppingcart.php?cid='.$i++.'"><img src="images/remove_x.gif" alt="remove" /><br />Remove</a> </td></tr>';
    }
    $_SESSION['checkoutCartTotal'] = $cartTotal;
    $cartOutput .= '<tr>
                        <td colspan="3" align="right"  height="40px">Have you modified your basket? Please click here to <input class="btn_upd" type="submit" name="btnUpdate" value="Update" />&nbsp;&nbsp;</td>
                        <td align="right" style="background:#ccc; font-weight:bold"> Total: </td>
                        <td colspan="2" align="left" style="background:#ccc; font-weight:bold;">$'.$cartTotal.' </td>
                        <td style="background:#ccc; font-weight:bold"> </td>
                    </tr>
                </table>

            <span style="color:#FF0000;"><p> *Note: If the item is not in stock, you should give us 12h to 24h to get it for you.</p></span>
                <div style="float:right; width: 215px; margin-top: 20px;">

                    <div class="checkout"><a href="checkout.php" class="more">Proceed to Checkout</a></div>

                </div></form>';
}

?>

The code above is the code of my cart, where is should sum the items. Look at this screen: http://prntscr.com/cn6vlx

This one is correct but if the sum reaches 1000 it will stop working:

http://prntscr.com/cn6vxe

And after reaching around 1000: http://prntscr.com/cn6wdg

  • 写回答

2条回答 默认 最新

  • duansao20000508 2016-09-28 10:34
    关注

    By default, number_format($pricetotal+$cartTotal,2) will not only truncate two decimals, but also add a thousands separator.

    So 999 + 1 will not give "1000.0" but "1,000.0" which will then fail the sum (since it is a string, not a "numbery" value. If it is treated as a number, it will be considered 1, just as 123,274 will be considered 123). So you have that 1000 + 1000 is actually "1,000.00" + 1000, and is treated as 1 + 1000; hence 1001, instead of the expected 2000.

    TL;DR only use number_format at the end of the script. If you need rounding, use round(), not number_format. At worst, maintain two different values - valueAsNumber (float) and valueAsString (string, formatted). You'll then have to keep them in sync.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?