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'=>999999999));

        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

Reply via email to