开发者

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.

E.g. 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.

M = The minimum points needed to gain the rank.

MX = The maximum points needed to gain the rank.

R = The Rank (e.g. Beginner, Master etc).

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:开发者_StackOverflow

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:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜