# algorithm – User Upgrade System

I've already asked this before, but I just want to re-encode it with a new upgrade format. What I want to do is that the user earns exp and once he has progressed, his previous experience will be added to his current exp.

So, to put it in a scenario it goes like this:
User1 is at level 1 with an exp_need of 1000 to reach level 2. He has gained an exp of 1000, so he is now at level 2 with a current_exp of 1000 and a necessary exp of 1400. Or if User1 has gained an exp of 1,100, it is at level 2 with a current_exp of 1,100 and 1,400 exp needed. And so on, according to the algorithm. But I get a result of -900 / 1,400 if the user wins 1,100 exp.

Here is the code below:

``````while (\$ user-> curr_exp> = \$ user-> exp_needed) {
\$ user-> curr_exp - = (\$ user-> exp_needed) + \$ user-> prev_exp;
\$ user-> prev_exp = \$ user-> exp_needed;
if (\$ user-> level <= 19){
\$user->exp_needed = \$ user-> prev_exp * 1.4;
}
elseif (\$ user-> level> = 20 && \$ user-> level <= 39){
\$user->exp_needed = \$ user-> prev_exp * 1.1;
}
elseif (\$ user-> level> = 40 && \$ user-> level <= 59){
\$user->exp_needed = \$ user-> prev_exp * 1.05;
}
elseif (\$ user-> level> = 60 && \$ user-> level <= 79){
\$user->exp_needed = \$ user-> prev_exp * 1.04;
}
elseif (\$ user-> level> = 80 && \$ user-> level <= 99){
\$user->exp_needed = \$ user-> prev_exp * 1.03;
}
elseif (\$ user-> level == 100) {
\$ user-> exp_needed = \$ user-> exp_needed * 0;
}
\$ user-> level + = 1;
\$ user-> save ();
}
``````

Ideas how to deal with this? Thank you.