RE: [PHP] Weight function.

From: Date: Thu, 17 Oct 2002 07:52:01 +0000
Subject: RE: [PHP] Weight function.
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-120294@lists.php.net to get a copy of this message
Thanks. I have just this minute got it working. Basically the same function I used for determining price of the cart items could be used for determining weight. All I needed to do was assign my DB with a field called weight and call that instead of price from the DB so my calculate weight function looks like this. function calculate_weight($cart) { // sum total weight for all items in shopping cart global $weight; $weight = 0.0; if(is_array($cart)) { $conn = db_connect(); foreach($cart as $ItemCode => $qty) { $query = "select weight from products where ItemCode='$ItemCode'"; $result = mysql_query($query); if ($result) { $item_weight = mysql_result($result, 0, "weight"); $weight +=$item_weight*$qty; } } } return $weight; } By making $weight a global variable I then call that in my shipping function and set it's parameters: function calculate_shipping_cost($weight) { //shipping costs calc. less than 10KG is 15 more than 10KG currently 20. global $shipping; if ($weight <= "10000") $shipping = "15"; else $shipping = "20"; return $shipping; } I still don't fully understand why this works but am happy it does!

« previous php.general (#120294) next »