[SOLVED] changing my rank system to work through database rather than sever side

Issue

This Content is from Stack Overflow. Question asked by MrUnique

Current system, rank and rankXP is in the users table. The rank names are determined by server side code same is levelup xp I am wondering best way to edit current code to run the ranks and levelup xp from another table in database so no matter weather later dates if names or levelup xp is changed its based off users xp so will revert automatically. this way it seems will only work if changed their xp up.

If easier to have server side but edit current code or best to rewrite.?

Current rank system

id username rank rankXP
1 myusername 0 231

// RANKS
$rank = $fetch['user_rank'];
$exp = $fetch["rank_xp"];
$rank_array = array("Tramp", "Crook", "Thief", "Hustler", "Mobster", "Soldier", "General", "Boss", "GodFather");
$rank_exp_array = array("750", "1600", "3400", "7000", "15000", "33000", "69000", "140000", "99999999999");
$rankdisplay = $rank_array[$rank];
$rankpercent = round($exp / $rank_exp_array[$rank] * 100,2);

// -- RANK SYSTEM -- 
if(($rank == "0") && ($exp >= "750")){ $newrank="1"; $done="1"; }
elseif(($rank == "1") && ($exp >= "1600")){ $newrank="2"; $done="1"; }
elseif(($rank == "2") && ($exp >= "3400")){ $newrank="3"; $done="1"; }
elseif(($rank == "3") && ($exp >= "7000")){ $newrank="4"; $done="1"; }
elseif(($rank == "4") && ($exp >= "15000")){ $newrank="5"; $done="1"; }
elseif(($rank == "5") && ($exp >= "33000")){ $newrank="6"; $done="1"; }
elseif(($rank == "6") && ($exp >= "69000")){ $newrank="7"; $done="1"; }
elseif(($rank == "7") && ($exp >= "140000")){ $newrank="8"; $done="1"; }
else{ $done = "0"; }

 if($done == "1"){
  mysqli_query($conn, "UPDATE Characters SET user_rank = '$newrank', money=money+ '50000', bullets=bullets+ '50' WHERE username= '$username'");
    
  }

I want to change my code so ranks run just off the rankXP in users

    id name level_up

    1 Tramp 750
    2 Crook 1600
    3 Thief 3400
    4 Hustler 7000
    5 Mobster 15000
    6 Soldier 33000
    7 General 69000
    8 Boss 140000
    9 Godfather 999999999 (Not to pass this rank)



Solution

Ok so just added another section to make the ranks go down without separate table for ranks in DB, seems to be working.

If there is a more cleaner way of doing what I have done I would very much be happy to take it on bored and accept the help.

Thanks.

if(($rank == "8") && ($exp < "140000")){ $newrank1="7"; $done1="1"; }
elseif(($rank == "7") && ($exp < "69000")){ $newrank1="6"; $done1="1"; }
elseif(($rank == "6") && ($exp < "33000")){ $newrank1="5"; $done1="1"; }
elseif(($rank == "5") && ($exp < "15000")){ $newrank1="4"; $done1="1"; }
elseif(($rank == "4") && ($exp < "7000")){ $newrank1="3"; $done1="1"; }
elseif(($rank == "3") && ($exp < "3400")){ $newrank1="2"; $done1="1"; }
elseif(($rank == "2") && ($exp < "1600")){ $newrank1="1"; $done1="1"; }
elseif(($rank == "1") && ($exp < "750")){ $newrank1="0"; $done1="0"; }

if($done1 == "1"){
  mysqli_query($conn, "UPDATE Characters SET user_rank = '$newrank1' WHERE username= '$username'");
    
  }


This Question was asked in StackOverflow by MrUnique and Answered by MrUnique It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?