Jump to content


Photo

ranking


  • Please log in to reply
2 replies to this topic

#1 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 11 June 2006 - 07:37 PM

i have two fields

name
points

i want to create a ranking on the points DESC, but if someone has the same points as someone else i want them to be the same rank.

any help please?

#2 birdie

birdie
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts
  • LocationBirmingham UK

Posted 11 June 2006 - 07:42 PM

$sql = "SELECT * FROM table ORDER BY points DESC";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query))
{
$points = $row['points'];
$person = $row['person'];
if($lastpersonspoints == $points)
{
$rank = $lastpersonsrank;
}
else
{
$rank = ""; //not sure how u are ordering this..
}
$lastpersonsrank = $rank;
$lastpersonspoints = $points;
}

That was completely off the top of my head so its probably got loads of errors. hope it gives u an idea of how u can do it :-)

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 12 June 2006 - 03:38 AM

try
$pos= $rank = 1;
$prev = 0;
$sql = "SELECT name, points FROM table ORDER BY points DESC";
$query = mysql_query($sql);
while(list($name, $pts) = mysql_fetch_row($query))    {
     $rank = $pts==$prev ? $rank : $pos;
     echo "$rank $n $v <br>";
     $pos++;
     $prev = $pts;
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users