Jump to content

need some help calculating


jwk811

Recommended Posts

i have a cart on my site and i have to figure out the total weight of the order using the weight and quantity..  so i get the weight and qty from the db which is in their cart and i need to multiply each weight and qty together and sum up all the products total weight.. understand? i think im going to need to use a for or while but im not sure and i dont really know how to do that.. thanks for any help!
Link to comment
Share on other sites

I think $weight1 = $item1*amount
$weight2 = $item2*amount
...  do this for every item then
$tweight = $weight1 + $weight2; ect.  You would then end up with the total weight, then just use your shopping cart script to find the price of the items.  If you used this way it would be simpler than looping.  In terms of optimization looping might be faster.  You will have to ask someone more experienced about that.
Link to comment
Share on other sites

ok my table it set up like this for the cart:
product_id/qty/session_id
then i find the weight of the product_id from another table- product:
product_id/weight/price
in the cart i need to multiply the qty by the weight for each product.. understand?

- and matt that would take too long and might not work out right.. hopin theres somethin easy to do this.. thanks!
Link to comment
Share on other sites

Something like this should work:

[code]
<?php
$sql = "SELECT * FROM `cart` WHERE `session_id`='$sessionid'";
$result = mysql_query($sql) or die(mysql_error());
$total_weight = 0;
while($row=mysql_fetch_assoc($result)){
$product_id = $row['product_id'];
$qty = $row['qty'];
$sql = "SELECT `weight` FROM `products` WHERE `product_id`='$product_id'";
$weight_result = mysql_query($sql) or die(mysql_error());
$weight = mysql_result($weight_result,0,"weight");
$item_weight = $weight * $qty;
$total_weight = $total_weight+$item_weight;
}
echo "Total weight: $total_weight";
?>
[/code]

Obviously you're gonna have to modify it a bit to work with your database and variables. Its also untested.
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.