We help IT Professionals succeed at work.

My PHP Shopping Cart won't allow me to exceed $999.99. How do I get it to go into the thousands?

marcleo
marcleo asked
on
To see what I'm talking about, go to http://www.onlineboosterclub.com/storefront. Select Youth Sports Foundation followed by any group and team that appears. Go to the T-shirts category and put 66 regular sized shirts in the cart. You'll see the total is $986.70. Update the cart to add the 67th shirt and the total goes to $1.00. What am I doing wrong? Below is the code that creates each table row of the shopping cart.
$subtotal = $price * $quantity;
$subtotal = number_format($subtotal,2);
$total += $subtotal;
$total = number_format($total,2);
$ucsize = strtoupper($size);
            
echo "<form action=\"index.php?org=$currentorgid&group=$currenttownid&team=$currentteamid\" method=\"post\" onsubmit=\"return formValidator()\">\n";
echo "<tr><td style=\"text-align: center\"><a href=\"index.php?org=$currentorgid&group=$currenttownid&team=$currentteamid&content=viewproduct&product=$apparelid&temporg=$orgid&tempgroup=$townid&tempteam=$teamid&tempapcolor=$nospaceapcolorname&tempdscolor=$nospacedscolorname\"><img src=\"images/designsonproduct/sm/$teamid-";

if ($designnumberlength == 1)
	echo "00";
else if  ($designnumberlength == 2)
	echo "0";
else if ($designnumberlength == 3)
	echo "";
echo "$designnumber-$nametouselow-$nospacedscolorname-$nospaceapcolorname-$cattouse.jpg\" style=\"width: 80px; height: 80px\" alt=\"$brand $shortdesc\" title=\"$brand $shortdesc\" /></a></td><td><a href=\"index.php?org=$currentorgid&group=$currenttownid&team=$currentteamid&content=viewproduct&product=$apparelid&temporg=$orgid&tempgroup=$townid&tempteam=$teamid&tempapcolor=$nospaceapcolorname&tempdscolor=$nospacedscolorname\">$brand $shortdesc</a></td><td>$ucsize</td><td>$nametouse<br />Design #$designnumber<br />$dscolorname on $apcolorname</td>";

printf("<td style=\"text-align: right\">$%.2f</td>\n", $price);
echo "<td><input type=\"text\" name=\"quantity\" id=\"quantity\" value=\"$quantity\" size=\"2\" maxlength=\"2\" /></td>\n";
printf("<td style=\"text-align: right\">$%.2f</td>\n", $subtotal);

echo "<td style=\"border: none\">\n";
echo "<input type=\"hidden\" name=\"content\" value=\"changeitem\">\n";
echo "<input type=\"hidden\" name=\"prodid\" value=\"$prodid\">\n";
echo "<input type=\"hidden\" name=\"sizeselection\" value=\"$size\">\n";
echo "<input type=\"submit\" name=\"button\" value=\"Update\"><br /><input type=\"submit\" name=\"button\" value=\"Remove\">\n";
echo "</td></tr>\n";
echo "</form>\n";

Open in new window

Comment
Watch Question

Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
If it's backed up by a database, it's likely to be the data length of one of your fields.


Author

Commented:
No, It doesn't get inserted in to the database until after you click Checkout then Confirm Order on the next page. The data type for the price field in my database is set to decimal(6,2).
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
OK.

Change the line that formats your number (2nd line of your code above) to this:

$subtotal = number_format($subtotal,2,'.','');

It's do do with the fact that you're formatting your number twice.

Take a look at the PHP man page for number_format, and read the first user submitted post to see what I mean.

http://php.net/manual/en/function.number-format.php


Author

Commented:
That works. I'm still trying to figure out how to add the comma between thousands. If I type

$subtotal = number_format($subtotal,2,'.',',');

it goes back to the way it was. I'm reading through this page right now: http://php.net/manual/en/function.number-format.php

Commented:
well, maybe instead of number_format, you could use money_format (http://www.php.net/manual/en/function.money-format.php) ;)
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Instead of using printf to format it a second time, just use echo

$subtotal = number_format($subtotal,2,'.',',');
echo "<p>$$subtotal</p>\n";




Author

Commented:
Chris, I tried that and the individual rows calculate correctly, but the total at the bottom stays at $1.00. Below is my updated code. I added in the code that displays the subtotal row.
$subtotal = $price * $quantity;
$subtotal = number_format($subtotal, 2, ".", ",");
$total += $subtotal;
$total = number_format($total, 2, ".", ",");
$ucsize = strtoupper($size);
            
echo "<form action=\"index.php?org=$currentorgid&group=$currenttownid&team=$currentteamid\" method=\"post\" onsubmit=\"return formValidator()\">\n";
echo "<tr><td style=\"text-align: center\"><a href=\"index.php?org=$currentorgid&group=$currenttownid&team=$currentteamid&content=viewproduct&product=$apparelid&temporg=$orgid&tempgroup=$townid&tempteam=$teamid&tempapcolor=$nospaceapcolorname&tempdscolor=$nospacedscolorname\"><img src=\"images/designsonproduct/sm/$teamid-";

if ($designnumberlength == 1)
	echo "00";
else if  ($designnumberlength == 2)
	echo "0";
else if ($designnumberlength == 3)
	echo "";

echo "$designnumber-$nametouselow-$nospacedscolorname-$nospaceapcolorname-$cattouse.jpg\" style=\"width: 80px; height: 80px\" alt=\"$brand $shortdesc\" title=\"$brand $shortdesc\" /></a></td><td><a href=\"index.php?org=$currentorgid&group=$currenttownid&team=$currentteamid&content=viewproduct&product=$apparelid&temporg=$orgid&tempgroup=$townid&tempteam=$teamid&tempapcolor=$nospaceapcolorname&tempdscolor=$nospacedscolorname\">$brand $shortdesc</a></td><td>$ucsize</td><td>$nametouse<br />Design #$designnumber<br />$dscolorname on $apcolorname</td><td style=\"text-align: right\">$$price</td><td><input type=\"text\" name=\"quantity\" id=\"quantity\" value=\"$quantity\" size=\"2\" maxlength=\"2\" /></td><td style=\"text-align: right\">$$subtotal</td>\n";

echo "<td style=\"border: none\">\n";
echo "<input type=\"hidden\" name=\"content\" value=\"changeitem\">\n";
echo "<input type=\"hidden\" name=\"prodid\" value=\"$prodid\">\n";
echo "<input type=\"hidden\" name=\"sizeselection\" value=\"$size\">\n";
echo "<input type=\"submit\" name=\"button\" value=\"Update\"><br /><input type=\"submit\" name=\"button\" value=\"Remove\">\n";
echo "</td></tr>\n";
echo "</form>\n";
}

echo "<tr><td style=\"text-align: right; border: none\" colspan=\"6\"><strong>Subtotal</strong></td><td style=\"text-align: right\"><strong>$$total</strong></td><td style=\"border: none\">&nbsp;</td></tr>\n";

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
It's the same problem as before - you're formatting the $total variable twice. You format the subtotal, add it to the total, and then apply the formatting again.

In your code, simply do the calculations before formatting the numbers. Swap lines 2 and 3 around.





$subtotal = $price * $quantity;
$total += $subtotal; //Do the calculations before using number_format
$subtotal = number_format($subtotal, 2, ".", ",");
$total = number_format($total, 2, ".", ",");
$ucsize = strtoupper($size);

Open in new window

Author

Commented:
Why is it that when one problem is fixed another occurs?

When there is more than one item in the cart it takes price * quantity and adds it to the total. Then it formats the total before looping to the second item. For instance, if the total is 1118.6 after the first product it formats it to 1,118.60 and goes to the next item.

Problem is that when the second loop happens, the program doesn't understand the formatted total and sets its value back to 0.

I have an idea for a solution that uses sizeof($_SESSION['cart']) and a for loop. If you have any other suggestions they are welcomed.
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
I would do the calculations on the raw numbers and only use the formatting functions when you need to display the results.

Because the variables in PHP aren't strongly typed (i.e your total variable isn't defined as any particular type), as soon as you start adding formatting to it, it may no longer act like a number - instead it'll act like a string.

In your loop simply do the calculations:

$subtotal = $price * $quantity;
$total += $subtotal;

Then only format them when you need to output them;

printf("<p>This is the formatted subtotal: $%s</p>", number_format($subtotal, 2, ".", ","));
printf("<p>This is the formatted total: $%s</p>",  number_format($total, 2, ".", ","));

Author

Commented:
I got it.

I added two variables before my foreach loop:
$i = 1; and
$sizecart = sizeof($_SESSION['cart']);

I changed the lines of math code to:
      $subtotal = $price * $quantity;
      $total += $subtotal;
      $tempsubtotal = number_format($subtotal,2,'.',',');
        if ($i == $sizecart)
                  $total = number_format($total,2,'.',',');

At the bottom of my foreach loop I added $i++; and I replaced the echo $subtotal to echo $tempsubtotal

Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
That seems a little over complicated. You've already got all the variables you need - $subtotal and $total. Just change the lines that echo them to include the formatting.