creativkook Posted June 26, 2007 Share Posted June 26, 2007 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. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 26, 2007 Share Posted June 26, 2007 SELECT sum(price) FROM table I saw an admin say that to a similar question. EDIT:SELECT sum(price) as total FROM table Quote Link to comment Share on other sites More sharing options...
craygo Posted June 26, 2007 Share Posted June 26, 2007 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 Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 26, 2007 Share Posted June 26, 2007 Yeah. Cause that query is MESSSEEEDD UP! LOL Quote Link to comment Share on other sites More sharing options...
Barand Posted June 26, 2007 Share Posted June 26, 2007 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' Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.