Jump to content

[SOLVED] Rank By Points issue


selliott

Recommended Posts

Hello,

    I put together some code that displays ranks, but if two people have the same amount of points (tied) I want them to have the same rank...then the next would skip a number.  For example, like this

 

Rank    Name    Points

1        Joe      105

2        John      101

2        Shawn  101

4        Sam      100

 

Right now, my code would just list the rankings for these members as 1,2,3,4

 

Here's my current code:

 

			<?php 

echo '<table cellpadding="3" cellspacing="0" style="width:100%;">
				<tr>
					<td>Rank</td><td>Driver</td><td>Points</td>
				</tr>';
$rank = 1;

while($r = mysql_fetch_array($Points)){ 					

	 echo '

	 <tr>
	 <td class="topline">' . $rank . '</td>
	 <td class="topline">' . $r['Name'] . '</td>
	 <td class="topline">' . $r['Points'] . '</td>
	 </tr>';

$rank++;

}

echo '</table>';

?>

Link to comment
Share on other sites

<?php
/*** begin mock db select ***/
for ($x = 1; $x <=20; $x++) {
   $array[] = rand(100,110);
}
sort($array);
/*** end mock db select *****/

$rank = 0;

foreach($array as $score) {
   $r = ($score == $oldScore)? $rank : ++$rank;
   echo $r . " " . $score . "<br/>";
   $oldScore = $score;
}
?>

 

Thanks for the reply.  What would I put in place of $array to get this to work with my connection? This is my SELECT statement:

 

$Points = mysql_query("SELECT ID, Name, Points FROM tq_Points ORDER BY Points DESC", $connection) or die("error querying database");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.