2016-12-16 197 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) –