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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.