Page 1 of 1

Quote calculation using a scale

Posted: Mon Aug 31, 2009 8:21 am
by bleen
Hi

I need to calculate a quote using a band/scale depending on number of people.

1 - 10 = 1.0 and 35
11 - 20 = 0.9 and 30
21 - 30 = 0.8 and 25
31 - 40 = 0.7 and 20
etc.

The user will inter the number of people and I will need to calculate an amount.

Example:

If the user enters 32, I will need to slot the people into the respective band i.e.

The first 10 will be: 10 x 1 x 35
The next 10 will be 10 x 0.9 x 30
The next 10 will be 10 x 0.8 x 25
and the remaining 2 will be 2 x 0.8 x 20

The value is the sum of each result.

Currently I would be using if and else statements to make this work and slot the values in the right band with the remainder in the last band.

Is there a better way of doing this such as arrays where it would fill up the first array 'pocket' with 10 and then slotting the remainder into the next 'pocket' etc.

This is hard to explain in a message so i hope I was clear.

Thanks

Robin

Re: Quote calculation using a scale

Posted: Mon Aug 31, 2009 8:38 am
by Eran
I can't tell from your description what is the source of data, so I wrote a possible generic solution:

Code: Select all

<?php
$scales = array(
    '1.0' => 35,
    '0.9' => 30,
    '0.8' => 25,
    '0.7' => 20
);
 
$number = 32;
$i = 1;
$scale = current($scales);
while($i <= $number) {
    $key = key($scales);
    $people = ($i + 10 < $number ? 10 : $number % 10);
    echo $i . " - " . ($i + $people - 1) . " = " . ($key * $scale * $people) . '<br />';
    $scale = next($scales);
    $i += 10;
}
you can replace $number with the actual people count, or alternatively iterate database rows in the while statement.