级别系统PHP?

I have attempted to build a level system for my users in php. So far I only have a mysql table of...

 id / xp (default 0) / xp_needed (default 5) / level (default 1)

I also have in my user's panel a bar which shows how much xp is needed, $details is an array of the mysql table columns.

$xp_needed = ($details['xp'] / $details ['xp_needed']) * 100;

echo'<p>XP Needed:</p><div class="bar Tooltip" title="' . $details['xp'] . '/' . 
$details['xp_needed'] . '"><span style="width: ' . $xp_needed . '%;"></span></div>';

What I can't work out is a function for checking if the xp is over the xp needed and if so updating the level to the next integer, updating the xp needed to 5 * the level and carrying over the remainder if any of the xp_needed to the new xp_needed?

Thanks for your patience

Write a function xpIncrease($amount), and call it everywhere you increase user's xp.

it should like this:

function xpIncrease($amount) {
if ($amount>=$details['xp_needed']) {
    $amount-=$details['xp_needed'];
    $details['xp_needed']=$newValue; //user leveled up, update xp_needed
    xpIcrease($amount); //call it again because user may level up twice or more...
} else {
    //just add it with SQL
}
}

I did mine like this..

        <?php
    $level_up = ($level + 1);
if ($exp >= $max_exp)
    {
    $sql = "UPDATE users SET level=(level + 1) , max_exp=(exp * 1.05) , skill_points=(skill_points + 3) WHERE id='".$id."' LIMIT 1";
    $res = mysql_query($sql);
if ($exp >= $max_exp)
        echo '<div class="Leveled">' . 'You sucessfully leveled up to ' . $level_up . '!' . ' As a reward you were given 3 skill points!' . '</div>';
    }
    else
    {
    } 
    ?>

Every time experience gets more than experienceneeded, person levels up then it sets experienceneeded * 1,2:

<?php include 'Style/database.php'; ?>
<?php include 'Style/functions.php'; ?>
    <?php   include'logged_in.php'; ?>      
<?php $randomnummer=rand(0,100); ?>

<?php   $sql = "SELECT * FROM users WHERE id = '$users[id]' LIMIT 1"; 
    if ($result = $mysqli->query($sql)) {$user = $result->fetch_array();} ?>



<?php if(isset($_POST['train']))
      { $msg="You are training!<br>" ;
$allowed = mysqli_query($mysqli," UPDATE users SET experience = experience+$randomnummer WHERE username = '$users[username]' ");
$level_up = mysqli_query($mysqli," UPDATE users SET experience = experience-experienceneeded ,level=level+1 WHERE username = 

$users[username]' ");


if ($users['experience'] >= $users['experienceneeded'])
{
$res = mysqli_query($mysqli,"UPDATE   users SET level = (level + 1) WHERE    id = '$users[id]' LIMIT    1");
$res = mysqli_query($mysqli,"UPDATE   users SET experienceneeded = experienceneeded * 1.2 WHERE    id = '$users[id]'LIMIT    1");
$res = mysqli_query($mysqli,"UPDATE   users SET credit = credit + 3  WHERE    id = '$users[id]'LIMIT    1");
           }
    if ($users['experience'] >= $users['experienceneeded'])
    { echo      '<div class="Leveled">'.
            'You sucessfully leveled up to '
            . $level_up . '!'.
             ' As a reward you were given 3 credit points!'
                    .'</div>';}}
 ?>


<div class="content">
<center>

<form method="post" action="user_train.php">
<?php echo "$msg" ; ?>
    <button type="submit" name="train" id="train">train for <?php echo $users['username'] ; ?></button>
</form></center>
</div>

It's as simple as:

while(currentExp >= expNeededNextLevel) {
currentExp -= expNeededNextLevel
levelUp();
}