Re: [PHP] Shipping Rates
At 2003-06-01 01:48 -0700, Ralph wrote: >Maybe it's just that it's late, but can't seem to figure this out. I >want to show a shipping price depending on the amount of purchase. I >thought about using a lot of if() statements, but I know this is not the >best way to go about this. > >Can anybody enlighten me on this one and give me a better approach. > >Here is an example of my shipping rates: > >Less than $20.00 = $7.45 >$20.01-$35.00 = $8.45 >$35.01- $55.00 = $9.45 >$55.01-$80.00 = $10.45 >$80.01-$100.00 = $11.45 >$100.01-$150.00 = $13.45 >$150.01-$200.00 = $15.55 >$200.01 or more = $19.45 Besides using a switch statement or a table you could use a formula. In your case (and most shipping rate cases like this) it's probably: shipping_rate=constant1+amount**constant2 Whereby '**' stands for 'to the power of', constant2 is somewhere between 0.55 and 0.75 and constant1 should ideally be 0 but you can raise it to increase the cost per shipment. If we use constant1=0 and constant2=0.65 we get: $10 -> $4.47 $20 -> $7.00 $30 -> $9.12 $175 -> $28.70 I propose that if you're interested you write a small PHP script that let's you experiment with different constants. You can also build in a round-up or round-down function to get nicer results. BTW. I think it might be better to charge the actual shipping costs and they are usually based on the weight of the packages and the stamps you have to put on them. You'd have to charge extra for handling of course. Greetings, Jaap -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Shipping Rates
> -Original Message- > From: Jim Lucas [mailto:[EMAIL PROTECTED] > Sent: 02 June 2003 18:07 > To: Jim Lucas; Rasmus Lerdorf; Ralph > > Answering my own answer. I see now how you are extracting > the sub array > information. > > My mistake. Notwithstanding that, I think your question was a good one -- it seems to me that the use of a nested array is unnecessary complex for this application; if we go with your simplified simple array: > > > > $rates = array('7.45'=>20, > >'8.45'=>35, > >'9.45'=>55, > >'10.45'=>80, > >'11.45'=>100, > >'13.45'=>150, > >'15.55'=>200, > >'19.45'=>9); then the foraech can surely be written very simply like this: foreach($rates as $i => $vals) { if($num <= $vals) { $price = $i; break; } } return $price; Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Shipping Rates
Answering my own answer. I see now how you are extracting the sub array information. My mistake. Jim Lucas - Original Message - From: "Jim Lucas" <[EMAIL PROTECTED]> To: "Rasmus Lerdorf" <[EMAIL PROTECTED]>; "Ralph" <[EMAIL PROTECTED]> Cc: "PHP General Mailing List" <[EMAIL PROTECTED]> Sent: Monday, June 02, 2003 9:57 AM Subject: Re: [PHP] Shipping Rates > is this correct? > > wouldn't you want the array to be like this? > > $rates = array('7.45'=>20, >'8.45'=>35, >'9.45'=>55, >'10.45'=>80, >'11.45'=>100, >'13.45'=>150, >'15.55'=>200, >'19.45'=>9); > > otherwise your foreach would need to be a for loop something like this. > > for($i=0; $i if( $num > current( key( $rates[$i] ) ) ) continue; > else { $price = key( $rates[$i] ); break; } > } > > Correct me if I am wrong. > > Jim Lucas > > - Original Message - > From: "Rasmus Lerdorf" <[EMAIL PROTECTED]> > To: "Ralph" <[EMAIL PROTECTED]> > Cc: "PHP General Mailing List" <[EMAIL PROTECTED]> > Sent: Sunday, June 01, 2003 2:26 AM > Subject: Re: [PHP] Shipping Rates > > > > Better approach in what sense? From a performance perspective you are not > > going to beat a specific set of if conditions unless there is an distinct > > formula you can apply. I will assume there is no simple mathematical > > relationship for your full set of data and what you are really looking for > > is a way to just feed the pricing structure into your code by some > > mechanism and then have it just work, so your goal is to make it more > > maintenable. To that end, you could stick your pricing table into an > > array of arrays and simple loop through it. Something like this: > > > > function find_rate($num) { > > $rates = array(array('7.45'=>20), > >array('8.45'=>35), > >array('9.45'=>55), > >array('10.45'=>80), > >array('11.45'=>100), > >array('13.45'=>150), > >array('15.55'=>200), > >array('19.45'=>9)); > > > > foreach($rates as $i => $vals) { > > if($num > current($vals)) continue; > > else { $price = key($vals); break; } > > } > > return $price; > > } > > > > This should return the correct shipping price for whatever you pass into > > find_rate(). > > > > -Rasmus > > > > On Sun, 1 Jun 2003, Ralph wrote: > > > > > Maybe it's just that it's late, but can't seem to figure this out. I > > > want to show a shipping price depending on the amount of purchase. I > > > thought about using a lot of if() statements, but I know this is not the > > > best way to go about this. > > > > > > Can anybody enlighten me on this one and give me a better approach. > > > > > > Here is an example of my shipping rates: > > > > > > Less than $20.00 = $7.45 > > > $20.01-$35.00 = $8.45 > > > $35.01- $55.00 = $9.45 > > > $55.01-$80.00 = $10.45 > > > $80.01-$100.00 = $11.45 > > > $100.01-$150.00 = $13.45 > > > $150.01-$200.00 = $15.55 > > > $200.01 or more = $19.45 > > > > > > Thanks. > > > > > > > > > > > > > > > > > > -- > > > 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 > > > > > > > -- > 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] Shipping Rates
is this correct? wouldn't you want the array to be like this? $rates = array('7.45'=>20, '8.45'=>35, '9.45'=>55, '10.45'=>80, '11.45'=>100, '13.45'=>150, '15.55'=>200, '19.45'=>9); otherwise your foreach would need to be a for loop something like this. for($i=0; $i current( key( $rates[$i] ) ) ) continue; else { $price = key( $rates[$i] ); break; } } Correct me if I am wrong. Jim Lucas - Original Message - From: "Rasmus Lerdorf" <[EMAIL PROTECTED]> To: "Ralph" <[EMAIL PROTECTED]> Cc: "PHP General Mailing List" <[EMAIL PROTECTED]> Sent: Sunday, June 01, 2003 2:26 AM Subject: Re: [PHP] Shipping Rates > Better approach in what sense? From a performance perspective you are not > going to beat a specific set of if conditions unless there is an distinct > formula you can apply. I will assume there is no simple mathematical > relationship for your full set of data and what you are really looking for > is a way to just feed the pricing structure into your code by some > mechanism and then have it just work, so your goal is to make it more > maintenable. To that end, you could stick your pricing table into an > array of arrays and simple loop through it. Something like this: > > function find_rate($num) { > $rates = array(array('7.45'=>20), >array('8.45'=>35), >array('9.45'=>55), >array('10.45'=>80), >array('11.45'=>100), >array('13.45'=>150), >array('15.55'=>200), >array('19.45'=>9)); > > foreach($rates as $i => $vals) { > if($num > current($vals)) continue; > else { $price = key($vals); break; } > } > return $price; > } > > This should return the correct shipping price for whatever you pass into > find_rate(). > > -Rasmus > > On Sun, 1 Jun 2003, Ralph wrote: > > > Maybe it's just that it's late, but can't seem to figure this out. I > > want to show a shipping price depending on the amount of purchase. I > > thought about using a lot of if() statements, but I know this is not the > > best way to go about this. > > > > Can anybody enlighten me on this one and give me a better approach. > > > > Here is an example of my shipping rates: > > > > Less than $20.00 = $7.45 > > $20.01-$35.00 = $8.45 > > $35.01- $55.00 = $9.45 > > $55.01-$80.00 = $10.45 > > $80.01-$100.00 = $11.45 > > $100.01-$150.00 = $13.45 > > $150.01-$200.00 = $15.55 > > $200.01 or more = $19.45 > > > > Thanks. > > > > > > > > > > > > -- > > 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 > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Shipping Rates
Better approach in what sense? From a performance perspective you are not going to beat a specific set of if conditions unless there is an distinct formula you can apply. I will assume there is no simple mathematical relationship for your full set of data and what you are really looking for is a way to just feed the pricing structure into your code by some mechanism and then have it just work, so your goal is to make it more maintenable. To that end, you could stick your pricing table into an array of arrays and simple loop through it. Something like this: function find_rate($num) { $rates = array(array('7.45'=>20), array('8.45'=>35), array('9.45'=>55), array('10.45'=>80), array('11.45'=>100), array('13.45'=>150), array('15.55'=>200), array('19.45'=>9)); foreach($rates as $i => $vals) { if($num > current($vals)) continue; else { $price = key($vals); break; } } return $price; } This should return the correct shipping price for whatever you pass into find_rate(). -Rasmus On Sun, 1 Jun 2003, Ralph wrote: > Maybe it's just that it's late, but can't seem to figure this out. I > want to show a shipping price depending on the amount of purchase. I > thought about using a lot of if() statements, but I know this is not the > best way to go about this. > > Can anybody enlighten me on this one and give me a better approach. > > Here is an example of my shipping rates: > > Less than $20.00 = $7.45 > $20.01-$35.00 = $8.45 > $35.01- $55.00 = $9.45 > $55.01-$80.00 = $10.45 > $80.01-$100.00 = $11.45 > $100.01-$150.00 = $13.45 > $150.01-$200.00 = $15.55 > $200.01 or more = $19.45 > > Thanks. > > > > > > -- > 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] Shipping Rates
I thought about this but how would I go about performing greater than or equal to without having to specify a CASE for every price range? -Original Message- From: Jason Paschal [mailto:[EMAIL PROTECTED] Sent: Sunday, June 01, 2003 1:47 AM To: [EMAIL PROTECTED] Subject: Re: [PHP] Shipping Rates this, maybe? http://www.php.net/switch >From: Ralph <[EMAIL PROTECTED]> >To: PHP General Mailing List <[EMAIL PROTECTED]> >Subject: [PHP] Shipping Rates >Date: Sun, 01 Jun 2003 01:48:30 -0700 > >Maybe it's just that it's late, but can't seem to figure this out. I >want to show a shipping price depending on the amount of purchase. I >thought about using a lot of if() statements, but I know this is not the >best way to go about this. > >Can anybody enlighten me on this one and give me a better approach. > >Here is an example of my shipping rates: > >Less than $20.00 = $7.45 >$20.01-$35.00 = $8.45 >$35.01- $55.00 = $9.45 >$55.01-$80.00 = $10.45 >$80.01-$100.00 = $11.45 >$100.01-$150.00 = $13.45 >$150.01-$200.00 = $15.55 >$200.01 or more = $19.45 > >Thanks. > > > > > >-- >PHP General Mailing List (http://www.php.net/) >To unsubscribe, visit: http://www.php.net/unsub.php > _ Add photos to your messages with MSN 8. Get 2 months FREE*. http://join.msn.com/?page=features/featuredemail -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Shipping Rates
Maybe it's just that it's late, but can't seem to figure this out. I want to show a shipping price depending on the amount of purchase. I thought about using a lot of if() statements, but I know this is not the best way to go about this. Can anybody enlighten me on this one and give me a better approach. Here is an example of my shipping rates: Less than $20.00 = $7.45 $20.01-$35.00 = $8.45 $35.01- $55.00 = $9.45 $55.01-$80.00 = $10.45 $80.01-$100.00 = $11.45 $100.01-$150.00 = $13.45 $150.01-$200.00 = $15.55 $200.01 or more = $19.45 Thanks. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php