[英]Javascript Count Price and quantity
Cart.PHP购物车.PHP
<div class="shopping-cart">
<section class="container content-section">
<h1>CART</h1>
<?php
$total = 0;
if (isset($_SESSION['cart'])){
$product_id = array_column($_SESSION['cart'], 'product_id');
$result = $db->getData();
while ($row = mysqli_fetch_assoc($result)){
foreach ($product_id as $id){
if ($row['id'] == $id){
cartElement($row['product_image'], $row['product_name'],$row['product_price'], $row['id']);
$total = $total + (int)$row['product_price'];
}
}
}
}else{
echo "<h5>Cart is Empty</h5>";
}
?>
<div class="cart-total">
<strong class="cart-total-title">Total</strong>
<span class="cart-total-price">$0</span>
</div>
<button class="btn btn-primary btn-purchase" type="button">CONFIRM ORDER</button>
</section>
</div>
component.php组件.php
function cartElement($productimg, $productname, $productprice, $productid){
$element = "
<form action=\"cart.php?action=remove&id=$productid\" method=\"post\" class=\"cart-items\">
<div class=\"cart-row\">
<span class=\"cart-item cart-header cart-column\">$productname</span>
<span class=\"cart-price cart-header cart-column\">RM$productprice</span>
<span class=\"cart-quantity cart-header cart-column\"><input type=\"number\" value=\"1\" min=\"1\"></span>
<button type=\"submit\" class=\"btn btn-danger mx-2\" name=\"remove\">Remove</button>
</div>
<div class=\"cart-items\">
</div>
</form>
";
echo $element;
}
Hello everyone, I would like to count the quantity and the price of it which is the total.大家好,我想数一下它的数量和价格,也就是总价。 Is it possible to do with javascript or php and how?
是否可以使用 javascript 或 php 以及如何使用? I have tried several ways to implement the function in javascript however none of my effort works.Thank you for helping me.
我已经尝试了几种方法来在 javascript 中实现该功能,但是我的努力都没有奏效。谢谢你帮助我。
i don't know how the cart stores data on the session, but doing in the following way will make it easy.我不知道购物车如何在会话中存储数据,但是按照以下方式进行操作会很容易。
$cartProducts = array_keys($_SESSION['cart']); $cartProducts = array_keys($_SESSION['cart']);
get product details from the DB something like:- where product_id
IN(implode(',',$cartProducts))"从数据库中获取产品详细信息,例如:- where
product_id
IN(implode(',',$cartProducts))"
loop through the products got from the DB遍历从数据库获得的产品
$total = 0;
while ($row = mysqli_fetch_assoc($result)){
$subtotal = $row['price'] * ($_SESSION['cart'][$row['id']]['quantity']);
$total += $subtotal;
// now you got product detail, quantity, price
// print the html formatted information here
}
at the end print the $total最后打印 $total
if you change the quantity - then update the session cart, and repeat the above.如果您更改数量 - 然后更新会话购物车,并重复上述操作。
Note:- it is just an example, you need to add validations, fomat/secure sql etc注意:- 这只是一个例子,您需要添加验证、格式/安全 sql 等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.