PHP总和达到1000时是错误的

时间:2016-09-28 10:09:14

标签: php mysql

我的购物车有一些问题,当我添加它时它没有对所有项目求和,并且值达到1000/1400左右,总和是错误的,它只是停止对项目求和。看看这张图片:http://prntscr.com/cn6uvg

在那里你可以看到2个值为1000的项目,它应该给出2000,但是它给出了1001,并且只有当总和大于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>';
}

?>

上面的代码是我的购物车的代码,其中应该是项目的总和。看看这个屏幕:http://prntscr.com/cn6vlx

这个是正确的,但如果总和达到1000,它将停止工作:

http://prntscr.com/cn6vxe

达到1000左右后:http://prntscr.com/cn6wdg

2 个答案:

答案 0 :(得分:3)

默认情况下,foo(a: SomeDataClass): Promise<void>{ if(!a){ return Promise.reject('a is undefined'); //<----- Promise.reject } return doSomething(a.value).then(()=>{ console.log('done'); }).catch((error)=>{ throw error; }); } 不仅会截断两位小数,还会添加千位分隔符

所以999 + 1不会给出&#34; 1000.0&#34;但&#34; 1,000.0&#34;然后它将失败总和(因为它是字符串,而不是&#34; numbery &#34;值。如果它被视为数字,它将被视为1,正如123,274将被视为123)。所以你有1000 + 1000实际上&#34; 1,000.00&#34; + 1000,被视为1 + 1000;因此1001,而不是预期的2000。

TL; DR 仅在脚本末尾使用number_format($pricetotal+$cartTotal,2)。如果您需要舍入,请使用number_format,而不是round()。在最坏的情况下,维护两个不同的值 - valueAsNumber(float)和valueAsString(字符串,格式化)。然后,您必须让它们保持同步。

答案 1 :(得分:0)

发现问题我刚刚删除了&#34; number_format&#34;它现在正在工作,不知道为什么,但它正在工作。