2017-09-27 149 views
-1

有趣的問題在這裏...不知道問題出在哪裏,因爲我正在使用我擁有的其他網站的代碼。第一個網站工作完美,沒有問題...但是當我將它轉換到我的新網站時,總價格數量將無法正確計算。PHP購物車總價格計算不正確

問題:當我切換代碼時,是否丟失了某些內容?如果我將下面的代碼放到while循環中,它只能用於一個項目,但不能用於多個項目,如果我將位置更改爲此項目,它看起來不正確。

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

下面是一些圖片和我的代碼:

enter image description here

enter image description here

OLD的工作代碼:

<?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> 
+0

注意:'mysqli'的面向對象的接口明顯不那麼冗長,使得代碼更易於閱讀和審計,並且不容易與陳舊的'mysql_query'接口混淆。在你過於投入程序風格之前,它是值得轉換的。例如:'$ db = new mysqli(...)'和'$ db-> prepare(「...」)過程接口是PHP4時代的一個神器,當引入mysqli API時,不應該在新的代碼:'mysql_error'是錯誤的函數在這裏使用 – tadman

+0

**警告**:當使用'mysqli'你應該使用[參數化查詢](http://php.net/manual/en/mysqli.quickstart。 prepared-statements.php)和['bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)將用戶數據添加到您的查詢。**不要**使用字符串插值或連接來完成這個工作,因爲你已經創建了一個嚴重的[SQL注入漏洞](http://bobby-tables.com/)。**從不**放置'$ _POST','$ _GET'或**任何**用戶數據直接加入到查詢中,如果有人試圖利用您的錯誤,這可能會非常有害。 – tadman

+0

許多問題可以通過[在mysqli中啓用例外]來檢測和解決(https:// stackoverflow。com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli),所以錯誤不容易被忽略。 – tadman

回答

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>'; 

記住,亂碼就是錯誤隱藏。保持乾淨,就好像你在廚房工作一樣。總是保持組織有序。如果你被困在一段代碼中,並不確定它爲什麼起作用,那麼你應該做的第一件事就是清理它。然後保持清潔。有時候在重新組織這個錯誤的過程中變得很明顯。

+0

謝謝你tadman,我會清理代碼,看看我能做些什麼。 –

0

於是我發現了問題......在我頁面的頂部我有這個代碼:

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

顯然是出於某種原因的代碼正在考慮它有2

$totalamount=0; 

像魔術現在工作正常,即使格式化的數字:有一次,我我while循環之前加入此向底部。我想保留格式化的數字,因爲我不想在我的價格中獲得任何美分,只需花費全部金額。