Jump to content

Finding cart total


creativkook

Recommended Posts

I have a question concerning my cart total.  I need to get the total  I have my products and cart in two separate tables:  products and cart.  In my cart table, I have stored the productid to tie it to the products table, userid from the session variable to tie the products in the cart together, and quantity.  In my products table I have the price for the product.  What would be a formula I could write to get the complete total dynamically? Because I could have one product up to 20.  If you need more info, let me know.  I thought about calculating the individual totals and then adding them, but I don't know how to do that.

Link to comment
https://forums.phpfreaks.com/topic/57323-finding-cart-total/
Share on other sites

do it up in the query

 

$sql = "SELECT SUM(unit_price*quantity) AS subtotal, field1, field2, userid FROM tablename WHERE userid = "someid"" GROUP BY userid;

 

then you can add the subtotals up using php

$total = 0;
while($r = mysql_fetch_assoc($result)){
echo $r['subtotal'];
$total += $r['subtotal'];
}
echo $total;

 

i have not tested the code at all but you should be able to figure out the way it works.

 

Ray

 

Link to comment
https://forums.phpfreaks.com/topic/57323-finding-cart-total/#findComment-283394
Share on other sites

Got anything constructive to add, Charlie, or just trying to boost post count?

 

 

Given your data is something like this

[pre]

cart                        product

----------                  ----------

userID            +-------  productID

productID  >-----+        price

qty

[/pre]

 

Then

 

SELECT SUM(p.price * c.qty) AS total
FROM cart c 
    INNER JOIN product p 
    ON c.productID = p.productID
WHERE c.userID = '$userID'

Link to comment
https://forums.phpfreaks.com/topic/57323-finding-cart-total/#findComment-283535
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.