RE: [PHP] Weight function.
I'm using both variables called in another page. Taking globals off causes weight not to be calculated in the shipping cost? Steve Jackson Web Developer Viola Systems Ltd. http://www.violasystems.com [EMAIL PROTECTED] Mobile +358 50 343 5159 > -Original Message- > From: Jason Wong [mailto:[EMAIL PROTECTED]] > Sent: 17. lokakuuta 2002 12:39 > To: [EMAIL PROTECTED] > Subject: Re: [PHP] Weight function. > > > On Thursday 17 October 2002 15:52, Steve Jackson wrote: > > 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; > > See below > > > } > > > > By making $weight a global variable I then call that in my shipping > > function and set it's parameters: > > By making $weight a global variable there is no need for > "return $weight;" > inside your function. And same for "return $shipping;" below. > > > > function calculate_shipping_cost($weight) > > { > > //shipping costs calc. less than 10KG is 15 more than > 10KG currently > > 20. > > > > global $shipping; > > if ($weight <= "1") > > $shipping = "15"; > > else > > $shipping = "20"; > > return $shipping; > > > > } > > > > I still don't fully understand why this works but am happy it does! > > -- > Jason Wong -> Gremlins Associates -> www.gremlins.com.hk > Open Source Software Systems Integrators > * Web Design & Hosting * Internet & Intranet Applications > Development * > > /* > The solution of problems is the most characteristic and > peculiar sort of voluntary thinking. > -- William James > */ > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Weight function.
On Thursday 17 October 2002 15:52, Steve Jackson wrote: > 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; See below > } > > By making $weight a global variable I then call that in my shipping > function and set it's parameters: By making $weight a global variable there is no need for "return $weight;" inside your function. And same for "return $shipping;" below. > function calculate_shipping_cost($weight) > { > //shipping costs calc. less than 10KG is 15 more than 10KG currently > 20. > > global $shipping; > if ($weight <= "1") > $shipping = "15"; > else > $shipping = "20"; > return $shipping; > > } > > I still don't fully understand why this works but am happy it does! -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* The solution of problems is the most characteristic and peculiar sort of voluntary thinking. -- William James */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Weight function.
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 <= "1") $shipping = "15"; else $shipping = "20"; return $shipping; } I still don't fully understand why this works but am happy it does! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Weight function.
On Thursday 17 October 2002 14:34, Steve Jackson wrote: > Hi all. > Wondering if anyone can shed some light! > > I have a problem whereby I need to define the weight of a selection of > items in a shopping cart in order to calculate a shipping cost. > Basically I have 5 products (at the moment) which are of set weights. > What I want to do is add up the amount of items in the cart and multiply > by weight then determine what the shipping weight is. Once that is > determined I can calculate the shipping cost. My problem is arrays. I > dunno what I'm doing with them! I can return the weight of one item and > multiply that by the amount of items in the cart but need to figure out > how do then do it again for any more items and add them to the first > calculation. Here is my code: > > function calculate_weight($items) > { > global $weight; > $conn = db_connect(); > $query = "select weight from products > where catid = $catid"; > $result = @mysql_query($query); > $weight = ($result*$items); > return $weight; > } > > Where $items is the number of items in the cart. $catid is the category > the products are in but maybe I need to use a different identifier. I > have another identifier called $itemCode which defines the actual item > which would probably be more reasonable? Your code as it stands does not do anything meaningful. 1) mysql_query() returns a resource_id -- which you assigned to $result. 2) You need to pass $result into one of these functions to actually get the data resulting from your query -- mysql_fetch_row(), mysql_fetch_array(), mysql_fetch_object(), mysql_result(). Try to get this part working then post your question again! -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* Absence makes the heart grow fonder. -- Sextus Aurelius */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Weight function.
Hi all. Wondering if anyone can shed some light! I have a problem whereby I need to define the weight of a selection of items in a shopping cart in order to calculate a shipping cost. Basically I have 5 products (at the moment) which are of set weights. What I want to do is add up the amount of items in the cart and multiply by weight then determine what the shipping weight is. Once that is determined I can calculate the shipping cost. My problem is arrays. I dunno what I'm doing with them! I can return the weight of one item and multiply that by the amount of items in the cart but need to figure out how do then do it again for any more items and add them to the first calculation. Here is my code: function calculate_weight($items) { global $weight; $conn = db_connect(); $query = "select weight from products where catid = $catid"; $result = @mysql_query($query); $weight = ($result*$items); return $weight; } Where $items is the number of items in the cart. $catid is the category the products are in but maybe I need to use a different identifier. I have another identifier called $itemCode which defines the actual item which would probably be more reasonable? Thoughts? Regards, Steve Jackson Web Developer Viola Systems Ltd. http://www.violasystems.com [EMAIL PROTECTED] Mobile +358 50 343 5159 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php