So you simply want to multiply the amount covered by each slab (“units”) with the rate then - so I would create an array that has the slab end as key, and the rate as value - then you can simply loop over that with the extended foreach syntax, and have access to the rate corresponding to the slab at all times.
Expanding on my answer to the previous question, this would look like this then:
$input = '128.82';
$slabs = [100 => 10, 150 => 12, PHP_INT_MAX => 14];
$result = [];
$bill = 0;
$previous_slab = 0;
foreach($slabs as $slab => $rate) {
// calculate distance between current and previous slab
$slab_distance = $slab - $previous_slab;
// if current remainder of input value is >= distance, add distance to result,
// and subtract distance from remainder of input
if( $input >= $slab_distance ) {
$result[] = $slab_distance;
$bill += $slab_distance * $rate;
$input -= $slab_distance;
}
// otherwise, add remainder as last item of result, and break out of the loop here
else {
$result[] = $input;
$bill += $input * $rate;
break;
}
$previous_slab = $slab;
}
var_dump($result, $bill);
Result:
array (size=2)
0 => int 100
1 => float 28.82
float 1345.84
My approach again uses only the upper slab boundaries here - you don’t have one in your last record from the database, so I am substituting PHP_INT_MAX here for that NULL value. Taking that into account, when you create that array from your database content, should be not challenge, I hope.
(If you only need the1345.84
as result here, and not the array that shows the distribution accross the indivudual slabs, then you can just remove all lines that contain$result
anywhere.)
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Yii is a simple yet high performance generic component framework based framework. It is known for its high performance, but above all, it is famous for its simplicity. This framework appeared in December 2008. It allows you to use third-party code, and its Gii code generator allows you to quickly create basic structures from which you can build your own solutions.
https://www.yiiframework.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.