Here is my function,
It is meant to get the user level an the amount of xp needed until the next level, it works but only through levels 1 to 2, then if the required xp for level 3 is entered it fails.
The XP doubles per level, so from level 1 to 2 is 10, 2 to 3 is 20, 3 to 4 is 40 etc;
$user[‘xp’] is an int to explain better, here are some examples of what the function returns with different values.
- $user[‘xp’] == 1, level 1, xpGot 1, xpNeeded 9, 10%
- $user[‘xp’] == 5, level 1, xpGot 5, xpNeeded 5, 50%
- $user[‘xp’] == 9, level 1, xpGot 9, xpNeeded 1, 90%
- $user[‘xp’] == 10, level 2, xpGot 0, xpNeeded 20, 0%
- $user[‘xp’] == 15, level 2, xpGot 5, xpNeeded 15, 25%
- $user[‘xp’] == 29, level 2, xpGot 19, xpNeeded 1, 95%
7. $user[‘xp’] == 30, level 2, xpGot 0, xpNeeded 0, 0%
It fails there on after.
function calculateLevel($xpGot) {
$level = 1;
$xpNeeded = 10;
while ($xpGot >= $xpNeeded) {
$level++;
$xpGot %= $xpNeeded;
$xpNeeded *= 2;
}
if ($xpGot < $xpNeeded) {
$xp = $xpGot / $xpNeeded * 100;
echo '<p>Level: ' . $level . '</p>';
echo '<div class="displayBarWrap" title="' . $xpGot . '/' . $xpNeeded . ' XP (' . $xp . '%)">
<p>XP:</p>
<div class="displayBar"><div style="width: ' . $xp . '%;"></div></div></div>';
}
}
calculateLevel($user['xp']);
I think you want following: replace
%=with-=: