Here's a working solution: I could have just selected the cart.quantity
attribute (considering how the products
and cart
table were joined):
$sql1 = "SELECT * FROM cart WHERE userid='$userId'"; //selects the user's cart
$query1=mysqli_query($con,$sql1);
if (!empty($query1)) { //if the user's cart isn't empty
while($row = mysqli_fetch_array($query1)){
$query = mysqli_query($con,"SELECT products.productImage1 , products.productName ,
products.id, cart.productId, cart.quantity as qty, products.productPrice,
products.shippingCharge FROM products JOIN cart ON cart.productid=products.id
ORDER BY cart.postingDate DESC"); //selects all the products in the current user's cart
}
$totalprice=0;
$totalqunty=0;
if (!empty($query)) {
while ($row = mysqli_fetch_array($query)) { //for each product
$quantity=$row['qty'];
$subtotal= $quantity*$row['productPrice']+$row['shippingCharge'];
$totalprice += $subtotal;
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…