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
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Increase non primary field

    Hi,

    I'm currently building a site for an online game and am almost complete. I have however run in to a bit of a wall. Players of the game can earn experience points and at certain point amounts they go up a level. What I want to know is this - is there a way to have a field (level) increase when another field (exp) reaches/passes a certain number?

    Thank you in advance.

  • #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,633
    Thanks
    4
    Thanked 148 Times in 139 Posts
    you can use a TRIGGER to do that.

  • #3
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    i did find a mention of that after google but couldnt work out how to do it. any help is appreciated

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    I wouldn't bother with a trigger.

    You can do this all in your SQL UPDATE statement.

    First, create a LEVELTABLE that holds the level numbers versus point ranges.
    Example:
    Code:
    CREATE levelTable ( level INT, levelName VARCHAR(30), minPoints INT, maxPoints INT );
    
    table contents example:
    
    level levelName minPoints maxPoints
         1 private          0        99
         2 corporal       100       399
         3 sergeant       400       799
         4 lieutenant     800      1499
    etc.
    And then, when you add points to user, do something like this:
    Code:
    UPDATE usertable AS U, levelTable AS L
    SET U.points = U.points + $addedPoints,
        U.userlevel = L.level
    WHERE (U.points + $addedPoints) BETWEEN L.minPoints AND L.maxPoints
    AND U.userid = $currentuser
    See? Do it all in one step.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Foster (11-16-2011)

  • #5
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    thank you that worked


  •  

    Posting Permissions

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