PHP Creating experienced leveis

Asked

Viewed 61 times

1

I want to create a general class that checks the experience of users in session.

Experience variables:

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

My question and goal is to create something like:

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

I started to create this way, but every time I do refresh it always adds +1 level. I wanted you to just add 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 experience and add the new level?

  • Use Array should solve your problem.

  • Well, what might happen is that you’re not zeroing in on the experience. Example: I have a table that has current Exp and max Exp, when it reaches the max Exp I reset the current Exp, and increase the max Exp. This avoids this problem.

  • hum ok understood, it is a good solution, and how can I increase the security of this script and updates?

2 answers

0

If it’s 50 to 50 you can just do:

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

Test it out here.

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


The floor round down, the rest is mathematical. Suppose exp be it 20 he will make (20/50) + 1 (=1.4), the floor will result in 1, this is independent of any refresh.

0

I appreciate the help, I managed to create an easier way and it works 100% which is to do update of Level, he makes a update where it withdraws (-250exp) and keeps the rest:

$update2 = "UPDATE users_steam SET exp = exp - '250' WHERE steamid = '$steamid'";
  • 1

    Do not use the answer space for new questions, but, not to be left unanswered, what is the return of $db->query?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.