Forum Moderators: coopster
Sorry if this is a bit broad, I'm just a bit unsure about how to go about this.
I would like to try and give rankings to certain members of a forum based on their points. But I want only the top 5 members to have their rankings displayed. For example, here is the query:
SELECT name,points FROM members ORDER BY points DESC LIMIT 5 foreach ($members_points as $member_point){
$points = $member_point->points;
}
echo 'points: '.$points; But I want it to display something like:
points : 100 (1st)
points : 75 (2nd)
points : 50 (3rd)
points : 25 (4th)
points : 1 (5th)
Thanks in advance.
// function to get ordinal suffix
function ordinal_suffix($n) {
$n_last = $n % 100;
if (($n_last > 10 && $n_last < 14) ¦¦ $n == 0){
return "{$n}th";
}
switch(substr($n, -1)) {
case '1': return "{$n}st";
case '2': return "{$n}nd";
case '3': return "{$n}rd";
default: return "{$n}th";
}
}//put query here
$members_points = etc...//initialize rank variable
$rank = 1;//begin loop
foreach ($members_points as $member_point){
echo 'points: '.$member_point->points.' ('.$rank.ordinal_suffix($rank).')<br>';//add 1 to rank
$rank++;
}