2016-12-16 196 views
0

這將顯示用戶所做的所有產品選擇,並且它可以工作。我需要添加一個函數的變量,該函數將返回所有$ total_price實例的總和,這樣我可以顯示所選產品(項目)的總價格。我相信對你們中的很多人來說這將是一件簡單的事情。 我不知道該怎麼做,也找不到一個例子。此代碼來自購物車,只要我可以顯示所有選擇的總價格,我就可以將其作爲結帳的一部分。 應該是一個簡單的事情,採取小計,並在此之後加稅。 有人請給我看$ sub_total的代碼嗎?在PHP while循環中獲取變量值的總和

<?php 
session_start(); 
//connect to database 
$mysqli = mysqli_connect("localhost", "root", "", "hestonw0355"); 
//$mysqli = mysqli_connect("localhost", "hestonw0355", "1Password!", "hestonw0355"); //for the school server 
//$mysqli = mysqli_connect("localhost", "sungr_RobW", "O+N7?Pa%Go*T&", "sungraff_hestonw0355") or die("Error " . mysqli_error($mysqli)); //for dailyrazor.com 

$display_block = "<h1>Your Shopping Cart</h1>"; 

//check for cart items based on user session id 
$get_cart_sql = "SELECT st.id, si.item_title, si.item_price, 
       st.sel_item_qty, st.sel_item_size, st.sel_item_color FROM 
       store_shoppertrack AS st LEFT JOIN store_items AS si ON 
       si.id = st.sel_item_id WHERE session_id = 
       '".$_COOKIE['PHPSESSID']."'"; 
$get_cart_res = mysqli_query($mysqli, $get_cart_sql) 
       or die(mysqli_error($mysqli)); 

if (mysqli_num_rows($get_cart_res) < 1) { 
    //print message 
    $display_block .= "<p>You have no items in your cart. 
    Please <a href=\"seestore.php\">continue to shop</a>!</p>"; 
} else { 

    while ($cart_info = mysqli_fetch_array($get_cart_res)) { 
     $id = $cart_info['id']; 
     $item_title = stripslashes($cart_info['item_title']); 
     $item_price = $cart_info['item_price']; 
     $item_qty = $cart_info['sel_item_qty']; 
     $item_color = $cart_info['sel_item_color']; 
     $item_size = $cart_info['sel_item_size']; 
     $total_price = sprintf("%.02f", $item_price * $item_qty); 
     $sub_total = 

    //get info and build cart display 
    $display_block .= <<<END_OF_TEXT 

    <table width='100%'> 
    <tr> 
    <th>Title</th> 
    <th>Size</th> 
    <th>Color</th> 
    <th>Price</th> 
    <th>Qty</th> 
    <th>Total Price</th> 
    <th>Action</th> 
    </tr> 
    <tr> 
    <td>$item_title <br></td> 
    <td>$item_size <br></td> 
    <td>$item_color <br></td> 
    <td>\$ $item_price <br></td> 
    <td>$item_qty <br></td> 
    <td>\$ $total_price</td> 
    <td><a href="removefromcart.php?id=$id">remove</a></td> 
    </tr> 
END_OF_TEXT; 
    } 
    $display_block .= "</table>"; 
} 
//free result 
mysqli_free_result($get_cart_res); 

//close connection to MySQL 
mysqli_close($mysqli); 
?> 

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Untitled Document</title> 
</head> 

<body> 
<?php echo $display_block; ?> 
</body> 
</html> 
+1

這是一種可怕的代碼風格。考慮將邏輯從標記中分離出來。單獨的HTML,PHP,JS,MySQL等 –

+1

請刪除您的密碼 –

回答

1

要獲得全球總價格,您可以:

設置一個變量$sub_totalwhile如下之前:

$sub_total = 0; 

while循環您添加到您的變量計算$total_price每次迭代如下:

$sub_total = $sub_total + $total_price 

或以緊湊的方式:

$sub_total += $total_price 

一個忠告:

改變這一點:

$total_price = sprintf("%.02f", $item_price * $item_qty); 

在:

$total_price = $item_price * $item_qty; 

,當你想顯示格式化值。

其他代碼部分應該改變,但我把注意力集中在你的主要要求上。

1

您可以設置

$sub_total = 0 before while loop starts; 

然後做$sub_total += $total_price。在while循環$ SUB_TOTAL將包含的所有產品總價結束。

+0

它的工程太棒了! ($ SUB_TOTAL = 0)和($ SUB_TOTAL + = $ TOTAL_PRICE)然後我把: \t \t 小計: \t​​\ $ $ SUB_TOTAL \t –

+0

它的偉大工程! ($ SUB_TOTAL = 0)和($ SUB_TOTAL + = $ TOTAL_PRICE)然後我把: \t \t 小計: \t​​\ $ $ sub_total \t .... - 並且顯示的項目的每個框都具有達到該點的小計。也許總計應該在頁面的不同區域顯示和更改,但這種方式非常棒。我會以同樣的方式繳納稅款。應該像($ grandtotal = sprintf(「%。02f」,$ sub_total * .06 + $ sub_total) –