dongyejun1983
dongyejun1983
2017-04-28 05:42

如何根据用户的经验为用户分配费率

已采纳

What syntax I should use to assign rate to user, if there would be 100 of levels. For aim to avoid hundred lines of code?

function showRank($practice) {

    $ranks = array(     "boy",
                        "student",
                        "master");

    $rank = $ranks[0];

    if ( $practice >= 10 and $practice < 20) {$rank = $ranks[1];}
    else if ($practice >= 20 and $practice < 30) {$rank = $ranks[2];}
    else if ($practice >= 30 and $practice < 40) {$rank = $ranks[3];}

    echo $rank;
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dpka7974 dpka7974 4年前

    If you have range of 10 then you can do it.like this

      function showRank($practice) {
    
        $ranks = array(     "boy",
                            "student",
                            "master");
    
        $rank = $ranks[0];
        foreach ($ranks as $key => $value) {
            $interval = ($key+1)*10;
            if($practice <= $interval)
            {
                $rank = $value;
                break;
            }
        }
    
    
        echo $rank;
    }
    showRank(20);
    
    点赞 评论 复制链接分享
  • dsd57259 dsd57259 4年前

    If your progression is at a constant rate, do it something like this: create $ranks array like you have

    $ranks = array("boy", "student", "master", .....);
    

    As indicated above, taking the progression of ranks to be 10 "points" each, then

    $rankId = floor($practice/10);
    $rank = $ranks[$rankId];
    
    点赞 评论 复制链接分享