PHP Creating experiences with experience

1

I want to create a general class that checks the session user experience.

Experience Variables:

$level1 = 0;
$level2 = 50;
$level3 = 100;
...

My question and goal is to create something of the sort:

if user tiver 0 exp, add lvl1
if user tiver 50 exp, add lvl2
if user tiver 100 exp, add lvl3
  ....

So I started to create this way, but whenever I do refresh it always adds +1 level. I wanted to add only 1x when having that experience:

if($level) {

    $level1 = 0;
    $level2 = 50;
    $level3 = 100;
    $level4 = 150;
    $level5 = 200;      
    $level6 = 250;
    $level6 = 300; 

    if($exp == $level2) {

         $update1 = "UPDATE users_steam SET level = level + '1' WHERE steamid = '$steamid'";
         $result1 = $db->query($update1); 

    } else if($exp == $level3) {

         $update2 = "UPDATE users_steam SET level = level + '1' WHERE steamid = '$steamid'";
         $result2 = $db->query($update2);
    }

}

How can I make it stop when I check the experiment and add the new level?

    
asked by anonymous 05.05.2017 / 22:26

2 answers

0

If it's 50 out of 50 you can simply do it:

$level = floor( ($exp / 50) + 1 );

Try this here.

This could be done at the time of displaying the user, whereas in the database it only keeps the value in experience.

The floor rounds down, the rest is math. Assuming exp is 20 it will (20/50) + 1 ( = 1.4 ), floor will result in 1 , this is independent of any refresh.     

05.05.2017 / 22:46
0

Thanks for the help, I managed to create an easier way and it works 100% which is when doing update of Level, it does a update where it removes (-250exp) and stays with the rest:

$update2 = "UPDATE users_steam SET exp = exp - '250' WHERE steamid = '$steamid'";
    
05.05.2017 / 23:33