


How does PHP quickly determine the user level based on user points?
Judge the level based on user points
1 2 3 4 5 6 |
|
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 |
|
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 |
|
1 2 3 4 5 6 7 8 |
|
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 |
|
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 |
|
)
99 points
Traverse the array
foreach($rules as $k=>$v){
1 2 3 |
|
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
效率不高,期待更好算法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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

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

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

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
