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" /> </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:
And after reaching around 1000: http://prntscr.com/cn6wdg
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.
Found the problem i just removed the "number_format
" and it is now working, not sure why but it's working.