jwk811 Posted January 14, 2007 Share Posted January 14, 2007 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! Quote Link to comment Share on other sites More sharing options...
mattd8752 Posted January 14, 2007 Share Posted January 14, 2007 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. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 14, 2007 Share Posted January 14, 2007 I think we're really going to need some information on your database structure to help you Quote Link to comment Share on other sites More sharing options...
jwk811 Posted January 14, 2007 Author Share Posted January 14, 2007 ok my table it set up like this for the cart:product_id/qty/session_idthen i find the weight of the product_id from another table- product:product_id/weight/pricein 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! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 14, 2007 Share Posted January 14, 2007 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. Quote Link to comment Share on other sites More sharing options...
jwk811 Posted January 14, 2007 Author Share Posted January 14, 2007 this came up.. mysql_result(): weight not found in MySQL result index 23 .. anyone know what the problem is? Quote Link to comment Share on other sites More sharing options...
jwk811 Posted January 14, 2007 Author Share Posted January 14, 2007 NEVERMIND GOT IT! THANKS GINGER! Quote Link to comment Share on other sites More sharing options...
Barand Posted January 14, 2007 Share Posted January 14, 2007 or single query[code]<?php$sql = "SELECT SUM(c.qty * p.weight) FROM cart c INNER JOIN product p ON c.product_id = p.product_id WHERE c.session_id = '$sessid' ";$res = mysql_query($sql) or die (mysql_error());$total_weight = mysql_result($res, 0);?>[/code] 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.