Table of Contents
Reply content:
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?

Aug 04, 2016 am 09:21 AM
php integral grade

Judge the level based on user points

1

2

3

4

5

6

<code>lv1:1~50

lv2:51~110

lv3:111~180

lv4:181~260

......

依次类推直到lv100</code>

Copy after login
Copy after login

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

1

2

3

4

5

6

<code>lv1:1~50

lv2:51~110

lv3:111~180

lv4:181~260

......

依次类推直到lv100</code>

Copy after login
Copy after login

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

1

(int)sqrt($onlinetime)+1;

Copy after login

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

1

2

3

4

5

6

7

8

<code><?php

function getLevel($point) {

    $level = 0;

    while($point >= 0) {

        $point -= 50 + $level++ * 10;

    }

    return $level;

}</code>

Copy after login

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

<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>

Copy after login

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(

1

2

3

<code>[1]=>array(1,50),

[2]=>array(51,110),

[3]=>array(111,180)</code>

Copy after login

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

1

2

3

<code>if($v[1]<99<$v[2]){

    //这里可以取出等级

}</code>

Copy after login

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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;

            }

        }

    }

Copy after login

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<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>

Copy after login
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles