获取PHP while循环中变量值的总和
这将显示用户所做的所有产品选择,并且可以正常工作。我需要添加一个带有函数的变量,该函数将返回$total_price的所有实例的总和,以便显示所选所有产品(项目)的总价。我相信对你们中的许多人来说,这将是一件简单的事情。 我不知道怎么做,也找不到一个例子。这段代码来自购物车,只要我能显示所有选择的总价格,我就可以将其用作结帐的一部分。 这应该是一个简单的事情,采取小计,并加上税之后。 有人能告诉我$sub_总数的代码吗获取PHP while循环中变量值的总和,php,html,mysql,Php,Html,Mysql,这将显示用户所做的所有产品选择,并且可以正常工作。我需要添加一个带有函数的变量,该函数将返回$total_price的所有实例的总和,以便显示所选所有产品(项目)的总价。我相信对你们中的许多人来说,这将是一件简单的事情。 我不知道怎么做,也找不到一个例子。这段代码来自购物车,只要我能显示所有选择的总价格,我就可以将其用作结帐的一部分。 这应该是一个简单的事情,采取小计,并加上税之后。 有人能告诉我$sub_总数的代码吗 <?php session_start(); //connect to
<?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>
无标题文件
要获取全球总价,您可以:
将变量$sub_total
设置在之前,而如下所示:
$sub_total = 0;
$sub_total = $sub_total + $total_price
在while
循环中,将为每次迭代计算的$total_price
添加到变量中,如下所示:
$sub_total = 0;
$sub_total = $sub_total + $total_price
或以紧凑的方式:
$sub_total += $total_price
忠告:
更改此项:
$total_price = sprintf("%.02f", $item_price * $item_qty);
在:
并在需要显示时设置值的格式
其他代码部分应该更改,但我将注意力集中在您的主要请求上。要获得全球总价,您可以:
将变量$sub_total
设置在之前,而如下所示:
$sub_total = 0;
$sub_total = $sub_total + $total_price
在while
循环中,将为每次迭代计算的$total_price
添加到变量中,如下所示:
$sub_total = 0;
$sub_total = $sub_total + $total_price
或以紧凑的方式:
$sub_total += $total_price
忠告:
更改此项:
$total_price = sprintf("%.02f", $item_price * $item_qty);
在:
并在需要显示时设置值的格式
其他代码部分应该更改,但我将注意力集中在您的主要请求上。您可以设置
$sub_total = 0 before while loop starts;
然后执行$sub\u total+=$total\u price
。在while循环结束时,$sub\u total将包含所有产品的总价。您可以设置
$sub_total = 0 before while loop starts;
然后执行$sub\u total+=$total\u price
。在while循环的最后,$sub\u total将包含所有产品的总价。这是一种可怕的代码样式。考虑把逻辑与标记损坏。分离html、php、js、mysql等,请删除密码这是一种可怕的代码风格。考虑把逻辑与标记损坏。单独的html、php、js、mysql等,请删除您的密码,这非常有用!($sub_total=0;)和($sub_total+=$total_price;)然后我输入:小计:\$$sub_total它工作得很好!($sub_total=0;)和($sub_total+=$total_price;)然后我输入:小计:\$$sub_total….并且显示的项目的每个框都有截至该点的小计。也许总数应该在页面的不同区域显示和更改,但这就是最好的方式。我将以同样的方式纳税。应该像($grandtotal=sprintf(%.02f),$sub_-total*.06+$sub_-total)它工作得很好!($sub_-total=0;)和($sub_-total+=$total-price;)然后我输入:小计:\$$sub-total它工作得很好!($sub_-total=0;)和($sub_-total+=$total-price;)然后我输入:小计:\$$sub\u total….显示的每个项目框都有截至该点的小计。也许总数应该在页面的不同区域显示和更改,但这就是最好的方式。我将以同样的方式输入税。应该像($grandtotal=sprintf(%.02f),$sub_总计*.06+$sub_总计)