Home >Backend Development >PHP Tutorial >How does PHP quickly determine the user level based on user points?

How does PHP quickly determine the user level based on user points?

WBOY
WBOYOriginal
2016-08-04 09:21:263658browse

Judge the level based on user points

<code>lv1:1~50
lv2:51~110
lv3:111~180
lv4:181~260
......
依次类推直到lv100</code>

How to quickly and efficiently determine the level based on the user’s points? It's okay to judge a few pieces of data if if, but if there are 100 ifs, the efficiency will drop. Does anyone have any good methods?

Reply content:

Judge the level based on user points

<code>lv1:1~50
lv2:51~110
lv3:111~180
lv4:181~260
......
依次类推直到lv100</code>

How to quickly and efficiently determine the level based on the user’s points? It's okay to judge a few pieces of data if if, but if there are 100 ifs, the efficiency will drop. Does anyone have any good methods?

Thank you very much to see that everyone is very active in helping me solve this problem. This column is called "Write Answers", which is not actually an answer, it just meets my needs.
This solution is actually to judge the user's level based on the user's online time, and upgrade to a certain level after meeting a certain length of time. Later, it was decided to use this method to judge the user's level time: the square of the current level is the next level. The number of hours required.
Finally, find the user level based on the duration

<code class="php">(int)sqrt($onlinetime)+1;</code>

How does PHP quickly determine the user level based on user points?

<code><?php
function getLevel($point) {
    $level = 0;
    while($point >= 0) {
        $point -= 50 + $level++ * 10;
    }
    return $level;
}</code>

It is recommended to maintain and store user level information directly in the database, otherwise using simple range query SQL may cause your index to fail.

If the data is irregular and you don’t want to save it in the database, use binary search to find the upper limit of points for the intermediate level of all levels. If the user’s points are greater than this upper limit, then recursively search for a level larger than this level. Otherwise, recursively search for a level smaller than this intermediate level. grade. .

The "points/50 algorithm" in the comments obviously cannot satisfy the poster's point level judgment, because the point level rules may change, and the algorithm itself has problems.

Ordered data search can be done using the binary method. Here is a simple implementation code for you

<code><?php

/**
 * 二分法查找
 *
 * @param int $score 积分
 * @param array $filter 积分规则
 *
 * @return array $filter
 */
function search($score, $filter)
{   
    $half = floor(count($filter) / 2); // 取出中間数

    // 判断积分在哪个区间
    if ($score <= $filter[$half - 1]['max']) {
        $filter = array_slice($filter, 0 , $half);
    } else {
        $filter = array_slice($filter, $half , count($filter));
    }

    // 继续递归直到只剩一个元素
    if (count($filter) != 1) {
        $filter = search($score, $filter);
    }

    return $filter;
}


$filter = [
    ['level' => 1, 'min' => 1, 'max' => 50],
    ['level' => 2, 'min' => 51, 'max' => 110],
    ['level' => 3, 'min' => 111, 'max' => 180],
    ['level' => 4, 'min' => 181, 'max' => 260],
    ['level' => 5, 'min' => 261, 'max' => 500],
];

$result = search(240, $filter);
echo current($result)['level'];

</code>

1. Add a redundant level field in the user table. Every time a new point is added, it will be judged by the way. If the level is improved, add 1. This is a real-time solution

2. If the delay in level promotion is allowed, it is recommended to use scheduled tasks or asynchronous queue processing calculations, because the points may still be quite loaded in the long run, which is considered to be planned in advance

I think if you really want to do this to get levels through points, it is recommended that you directly define all key-value pairs and use space to exchange time. The actual business logic does not have many levels and does not require any algorithms at all. This is the highest performance method. You can write the code to generate this array yourself
$arr[1]=“lv1”;$arr[2]=“lv1”;....$arr[50 ]="lv1";
$arr[51]="lv2";$arr[52]="lv2";....$arr[110]="lv2";
...
. ..
...
...

What about the rules? Without rules, there is no rule

= = The first idea was to use the switch statement. After watching the second floor, I felt that I was low and I still need to continue learning

Use a database, it’s more reliable. The performance of loops and if judgments is not reached. The database stores 100 small cases of data, and querying is easy. In addition, the pattern of your data is not suitable for judgment in the program.

I adopted the first solution upstairs

$rules = array(

<code>[1]=>array(1,50),
[2]=>array(51,110),
[3]=>array(111,180)</code>

)
99 points
Traverse the array
foreach($rules as $k=>$v){

<code>if($v[1]<99<$v[2]){
    //这里可以取出等级
}</code>

}

<code class="php">function getLevel($point)
    {
        $level = [
            0 => ['max' => 1000, 'min' => 0, 'name' => '新手', 'level' => 1],
            1 => ['max' => 10000, 'min' => 1000, 'name' => '小将', 'level' => 2],
            2 => ['max' => 20000, 'min' => 10000, 'name' => '中将', 'level' => 3],
            3 => ['max' => 50000, 'min' => 20000, 'name' => '上将', 'level' => 4],
            4 => ['max' => 100000, 'min' => 50000, 'name' => '大将', 'level' => 5],
            5 => ['max' => 999999999, 'min' => 100000, 'name' => '将军', 'level' => 6],
        ];
        foreach ($level as $value) {
            if (($point >= $value['min']) && ($point < $value['max'])) {
                return $value;
            }
        }
    }</code>

效率不高,期待更好算法。

<code>public function gradefun($gf)//用户等级函数
{
    $arr = array(120000 => 12, 80000 => 11, 50000 => 10, 30000 => 9, 12000 => 8, 8000 => 7, 5000 => 6, 2500 => 5, 1200 => 4, 500 => 3, 100 =>2, 0 => 1);
    
    foreach ($arr as $key => $value)
    {
        if ($gf >= $key)
        {
            return $value;
        }
    }
}




这个可以解决等级数小的,大的不清楚,没用过。</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn