Question

I've been trying for a couple of weeks to figure out how to solve this problem but I can't get it right...

I have a table in a database that consists of a monthdate, user id, target and value as shown below.

+-------+---------+--------+-------+
| month | user_id | target | value |
+-------+---------+--------+-------+
| Jan   |       1 |  10000 |  7694 |
| Feb   |       1 |  10000 |  9487 |
| Mar   |       1 |  12000 | 14054 |
+-------+---------+--------+-------+

Displaying the data is the easy part but it gets tricky when overflow is involved. If the user's value exceeds the target for the month, then the leftover overflows onto the next month and continues to fill up throughout the whole year. For example, if the target for Jan is £10,000 and the value is £35,000 at the end of Jan, then Jan: 100%, Feb: 100%, Mar: 100%, Apr: 50% (assuming that the target stays the same throughout the year).

If the user does not exceed the target for the previous month, then for the next month the value will be added to the previous month so that the target is met.

Is there a simpler way to do this using PHP & MySQL? Right now I'm simply querying the data and then displaying it using multiple if conditions and displaying the overflow using while loops. Should I instead be using arrays?

I'm not expecting explicit code but if you could point me in the right direction I would really appreciate it! :)

Was it helpful?

Solution

Without seeing any php code it is difficult for me to give you much help. This problem you are having seems pretty straight forward. From what I gather the Target and the Value should be the same number except for the last month where it could be less. Just doing some simple math inside of a loop will be enough to accomplish what you're doing. if value > target ... $overflow = $value - target; ... next loop if overflow > 0 .. $value += $overflow.. rinse & repeat

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top