作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个简单的 php 购物车:
我想计算购物车中所有商品的总价,但我不知道如何计算?
我用这段代码显示项目:
<?php
session_start();
$where = implode(",",$_SESSION['product']);
$get_p = $mysqli->prepare("select id,name,price,imgs,quantity from products where id IN ($where)");
$get_p->execute();
$get_p->bind_result($id,$name,$price,$imgs,$quantity);
$get_p->store_result();
while($get_p->fetch()) {
$qty = $_SESSION["qty"][$id];
$total = $qty * $price;
?>
<tr>
<td><img src="<?php echo htmlspecialchars($imgs); ?>"/></td>
<td><?php echo htmlspecialchars($name); ?></td>
<td><?php echo htmlspecialchars($price)."×".$qty; ?></td>
<td><input type="number" value="<?php echo $qty; ?>" min="1" name="qty_cart"></td>
<td><input type="submit" value="delete" name="delete<?php echo $id; ?>"/></td>
<?php
if(isset($_POST["delete".$id])) {
unset($_SESSION['product'][$id]);
}
?>
</tr>
<?php
}
?>
</table>
<div class="price_all_items">
</div>
<input type="submit" name="sub" value=""/>
</form>
最佳答案
替换这部分代码
while($get_p->fetch()) {
$qty = $_SESSION["qty"][$id];
$total = $qty * $price;
有了这个
$totalSum = 0;
while($get_p->fetch()) {
$qty = $_SESSION["qty"][$id];
$total = $qty * $price;
$totalSum += $total;
在循环结束后,$total
中的值将是总金额。
关于php - 用php计算元素的总价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38286918/
import java.util.Scanner; import java.util.Date; import java.text.DateFormat; import java.text.Simpl
您将看到示例代码。 以下是我的问题:-我必须选择有值(value)的服务,但是当我取消服务时,总价格增加而不是减少。-我测试后取消了所有服务,但是总价还是有。我怎样才能让它回到零? 谢谢! :) 这是
我是一名优秀的程序员,十分优秀!