Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2010
    Posts
    35
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Smile PHP Level system with user XP

    This is my code:
    PHP Code:
    <?php
    if ($userexp 50) {
        
    $sfl_level="1";
    }else if (
    $userexp 75){
    $sfl_level "2";
    } else {
    $sfl_level "0";    
    }
    ?>
    I am attempting to develop a user level system for my site, users already have an exp value stored in the database. I am wanting to make some code which gives people a level depending on how much xp they have. For example if they have more than 50 xp they become level 1 and if they have more than 75 xp they will become level 2. When I have attempted to use this system a user with 90 xp is only given a level of 1 instead of 2.

    I need to find a way to make all the levels seperate and work properly, and I want people with less than 50 xp to be level zero.

    Any help with this would be greatly appreciated,
    Thank you
    Last edited by mrkfc; 02-18-2012 at 12:51 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    The problem is this if. If you have 100, 100 is > 50, so the first if branch applies, not the else if or the else. When seeking a > check, start at the top down.
    The better solution is to write an algorithm to calculate the end $sfl_level based on the provided $userexp instead of using hardcoded numbers. This takes some trig knowledge to do properly if you want to avoid linear growth (which with exp would tell me that you are looking for each level being more difficult to obtain than the previous, although the numbers you have here indicate that the amount requires is reduced as levels increase).

  • Users who have thanked Fou-Lu for this post:

    mrkfc (02-18-2012)

  • #3
    New Coder
    Join Date
    Sep 2010
    Posts
    34
    Thanks
    22
    Thanked 0 Times in 0 Posts
    This looks like something I would use a switch statement for.
    http://www.w3schools.com/php/php_switch.asp

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,611
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Put the if tests in descending order (no need for a switch statement as that would need to add a second test for each value to provide the other end of the range)

    PHP Code:
    <?php 
    if ($userexp 75) { 
        
    $sfl_level="2"
    }else if (
    $userexp 50){ 
    $sfl_level "1"
    } else { 
    $sfl_level "0";     

    ?>
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    mrkfc (02-18-2012)

  • #5
    New Coder
    Join Date
    Aug 2010
    Posts
    35
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thank you


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •