简体   繁体   English

一个更简单或更有效的方法?

[英]A easier or better way of doing this?

I'm currenty working on a website (which consists of cms with a forum), and I'm wanting to give each user points for doing specific tasks (++ everytime), which should eventually give them a rank. 我目前正在一个网站上(该网站由带有论坛的cms组成),并且我想为每个用户提供执行特定任务的积分(每次++),这最终应该给他们一个排名。

Eg if the user did a post they'd get 5 points, if the user submitted a thread/topic they'd get 20 points and so on and the ranks would be like if the points are over M and lower then MX they'd be the R rank and so on. 例如,如果用户发表了一篇文章,他们将获得5分,如果用户提交了主题/主题,他们将获得20分,依此类推,排名就好比如果分数超过M并低于MX,是R等级,依此类推。

M = The minimum points needed to gain the rank. M =获得排名所需的最低分。

MX = The maximum points needed to gain the rank. MX =获得排名所需的最高分。

R = The Rank (eg Beginner, Master etc). R =等级(例如,初学者,硕士等)。

Hope all is clear, its a bit like the traditional forum points system where your given points for posts - which would give them a Display Title/Rank. 希望大家都清楚,有点像传统的论坛积分系统,在该系统中,您给帖子指定的积分-可以为他们显示标题/排名。

I'm currently storing the points within a MySQL DB, and assigning a Rank via PHP with if statements like so: 我目前正在将点存储在MySQL数据库中,并通过带有if语句的PHP分配等级,如下所示:

if ($points > 0 && < 100) {  
    $rank = 'Beginner';  
}  

Is their some sort of class, library, function, formula, method; 是他们的某种类,库,函数,公式,方法吗? you can suggest which could make this easier, or a better way you can think of? 您可以建议采用哪种方法可以使此操作更轻松,或者可以想到一种更好的方法?

Cheers! 干杯!

You don't need to store both the min and max. 您无需同时存储最小值和最大值。 (Unless the point values for your ranks are non-contiguous, I suppose.) I'd do this by putting your ranks in an array, indexed by the min value for that rank, in reverse order: (我想除非您的等级的点值是不连续的。)我可以通过将您的等级以相反的顺序放置在一个数组中,该数组由该等级的min值索引来实现:

$ranks = array(
    500 => 'Cthuloid',
    250 => 'Veteran',
    100 => 'Beginner',
    50 => 'Noob'
);

Then just write a short-circuiting loop to find the first rank that that's greater than or equal to the user's score: 然后,只需编写一个短路循环即可找到大于或等于用户分数的第一个等级:

function getRankOfScore($score)
{
    foreach ($ranks as $value => $name) {
        if ($score >= $value) {
            return $name;
        }
    }
    return 'Unranked';
}

Seems like it might be better to store the rank in the DB, and update it whenever you update the point value. 似乎最好将等级存储在数据库中,并在每次更新点值时对其进行更新。 That way you aren't computing the rank for every user on every page view. 这样,您就不必计算每个页面视图上每个用户的排名。 Views will happen much more frequently than updates. 视图发生的频率将比更新发生的频率高得多。

I don't think that's what you were asking. 我想这不是你要的。 Just my two cents. 只是我的两分钱。

EDIT : On second look, I see that there is nothing in the post to suggest you weren't already doing that in the first place. 编辑 :从第二个角度看,我发现帖子中没有任何内容建议您一开始就没有这样做。 Feel free to ignore me. 别理我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM