PHP购物车总价格无法正确计算

时间:2017-09-27 01:07:19

标签: php mysql shopping-cart

这里有趣的问题......不知道问题出在哪里,因为我使用的是其他网站的代码。第一个网站运行完美,没有问题...但是当我将其转换到我的新网站时,总价格金额将无法正常计算。

问题:我在切换代码时遗漏了什么?如果我将下面的代码放入while循环中,它只能使用一个项目,但不是多个,如果我将位置更改为此项,它看起来不正确。

<div class="column text-lg">Subtotal: <span class="text-medium">$<?php echo $totalamount; ?></span></div>

以下是我的一些图片和代码:

enter image description here

enter image description here

旧工作代码:

<?php
if ( ! isset($totalamount)) {
$totalamount=0;
}
$totalquantity=0;
if (!session_id()) {
session_start();
}
include ('core/connectdb.php');
$sessid = session_id();
$query = "SELECT * FROM cart WHERE cart_sess = '$sessid'";
$results = mysqli_query($connect, $query) or die (mysql_query());
if(mysqli_num_rows($results)==0)
{
echo '<div id="content" class="float_r"><div align="center"><h3>Your cart is empty.</h3> You can find our items on our <a href="products.php">product page</a>.</div></div><div class="cleaner"></div>';
}
else
{
?>
<div id="content" class="float_r">
<div align="center"><h1>Shopping Cart</h1></div>
<table border="1" align="center" cellpadding="5">
<tr><td> Item Code</td><td>Quantity</td><td>Item Name</td><td>Price</
td><td>Total Price</td>
<?php
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
extract($row);
echo "<tr><td>";
echo $cart_itemcode;
echo "</td>";
echo "<td><form method=\"POST\" action=\"cart.php?action=change&icode=
$cart_itemcode\"><input type=\"text\" name=\"modified_quantity\" size=\"2\"
value=\"$cart_quantity\">";
echo "</td><td>";
echo $cart_item_name;
echo "</td><td>";
echo '$' . $cart_price . '';
echo "</td><td>";
$totalquantity = $totalquantity + $cart_quantity;
$totalprice = number_format($cart_price * $cart_quantity, 2);
$totalamount=$totalamount + ($cart_price * $cart_quantity);
echo '$' . $totalprice . '';
echo "</td><td>";
echo "<input type=\"submit\" name=\"Submit\" value=\"Change quantity\">
</form></td>";
echo "<td>";
echo "<form method=\"POST\" action=\"cart.php?action=delete&icode=$cart_itemcode\">";
echo "<input type=\"submit\" name=\"Submit\" value=\"Delete Item\"></form>
</td></tr>";
}
echo "<tr><td >Total</td><td>$totalquantity</td><td></td><td></td><td>";
$totalamount = number_format($totalamount, 2);
echo '$' . $totalamount . '';
echo "</td></tr>";
echo "</table><br>";
echo "<div style=\"width:400px; margin:auto;\">You currently have " .
$totalquantity . " product(s) selected in your cart</div> ";
?>
<table border="0" style="margin:auto;">
<tr>
<td><button style="font-family:verdana; font-size:150%;" onclick="goBack()">Go Back</button></td>
<td style="padding: 10px;">
<form method="POST" action="cart.php?action=empty">
<input type="submit" name="Submit" value="Empty Cart"
style="font-family:verdana; font-size:150%;" >
</form>
</td><td>
<?php include('cart_upload.php'); ?>
</td></tr></table>
</div>
<div class="cleaner"></div>
<?php
}
?>

新的非工作代码:

<?php
if ( ! isset($totalamount)) {
$totalamount=0;
}
$totalquantity=0;
if (!session_id()) {
session_start();
}
include ('core/connectdb.php');
$sessid = session_id();
$query = "SELECT * FROM cart WHERE cart_sess = '$sessid'";
$results = mysqli_query($connect, $query) or die (mysql_query());
if(mysqli_num_rows($results)==0)
{
echo '<div"><div align="center"><h3>Your cart is empty.</h3> You can find our items on our <a href="products.php">product page</a>.</div></div>';
}
else
{
?>
    <!-- Page Title-->
      <div class="page-title">
        <div class="container">
          <div class="column">
            <h1>Cart</h1>
          </div>
          <div class="column">
            <ul class="breadcrumbs">
              <li><a href="index.php">Home</a>
              </li>
              <li class="separator">&nbsp;</li>
              <li>Cart</li>
            </ul>
          </div>
        </div>
      </div>
      <!-- Page Content-->
      <div class="container padding-bottom-3x mb-1">
        <!-- Shopping Cart-->
        <div class="table-responsive shopping-cart">
          <table class="table">
            <thead>
              <tr>
                <th>Product Name</th>
                <th class="text-center">Quantity</th>
                <th class="text-center">Subtotal</th>
                <th class="text-center"><a class="btn btn-sm btn-outline-danger" href="#">Clear Cart</a></th>
              </tr>
            </thead>
            <tdbody>
            <?php
            while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
            extract($row);
            $cart_price = number_format($cart_price);
              echo '<tr>';
                echo '<td>';
                  echo '<div class="product-item"><a class="product-thumb" href="shop-single.php?item=' . $cart_itemcode . ' "><img src="' . $cart_imagename . '" alt="' . $cart_item_name . '"></a>';
                    echo '<div class="product-info">';
                      echo '<h4 class="product-title"><a href="shop-single.php?item=' . $cart_itemcode . ' ">' . $cart_item_name . '</a></h4>';
                    echo '</div>';
                  echo '</div>';
                echo '</td>';
                echo '<td class="text-center">';
                  echo '<div class="count-input">';
                  echo '$' . $cart_price . ' Each';
                      echo "<form method=\"POST\" action=\"cart.php?action=change&icode=
                            $cart_itemcode\"><input type=\"text\" name=\"modified_quantity\" size=\"2\"
                            value=\"$cart_quantity\"><br\><input type=\"submit\" name=\"Submit\" value=\"Update\">
                            </form>";
                  echo '</div>';
                echo '</td>';
                $totalquantity = $totalquantity + $cart_quantity;
                $totalprice = number_format($cart_price * $cart_quantity);
                $totalamount= number_format($totalamount + ($cart_price * $cart_quantity));
                echo '<td class="text-center text-lg text-medium">$' . $totalprice . '</td>';
                echo '<td class="text-center"><a class="remove-from-cart" href="cart.php?action=delete&icode=' . $cart_itemcode . '" data-toggle="tooltip" title="Remove item"><i class="icon-cross"></i></a></td>';
              echo '</tr>';
            }
}
?>
</tbody>
</table>
</div>
<div class="shopping-cart-footer">
<div class="column text-lg">Subtotal: <span class="text-medium">$<?php echo $totalamount; ?></span></div>
</div>
<div class="shopping-cart-footer">
<div class="column"><a class="btn btn-outline-secondary" onclick="goBack()"><i class="icon-arrow-left"></i>&nbsp;Back to Shopping</a></div>
<div class="column"><a class="btn btn-primary" href="#" data-toast data-toast-type="success" data-toast-position="topRight" data-toast-icon="icon-circle-check" data-toast-title="Your cart" data-toast-message="is updated successfully!">Update Cart</a><a class="btn btn-success" href="checkout-address.php">Checkout</a></div>
</div>
</div>

2 个答案:

答案 0 :(得分:1)

看起来你正在将格式化数字加在一起,这肯定会有麻烦。不要这样做。在内部保持您的值尽可能原始,只有在您向用户显示时才格式化:

$totalquantity = $totalquantity + $cart_quantity;
$totalprice = $cart_price * $cart_quantity;
$totalamount = $totalamount + ($cart_price * $cart_quantity);

echo '<td class="text-center text-lg text-medium">$' . number_format($totalprice) . '</td>';

请记住,杂乱的代码是错误隐藏的地方。保持清洁,就好像你在厨房里工作一样。始终,始终保持井井有条。如果您遇到大量代码并且无法确定其工作原理,那么您应该做的第一件事就是清理它。然后继续清洁。有时在重新组织过程中,错误变得明显。

答案 1 :(得分:0)

所以我发现了问题...在我的页面顶部我有这个代码:

if ( ! isset($totalamount)) {
  $totalamount=0;
}

由于某种原因,代码正在考虑将其值设为2。

在我的while循环之前将其添加到底部:

$totalamount=0;

就像魔术一样,即使格式化的数字也能正常工作。我想保留格式化的数字,因为我不想要我的价格中的任何美分,只需要全部金额。