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
https://forums.phpfreaks.com/topic/141547-solved-rank-by-points-issue/
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;
}
?>

<?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");

you would query your db and use your while loop instead of the foreach loop and check your current row's score against your previous row's score.  Come on man I showed you how to pee in the toilet; you want me to unzip your fly and hold your willy too?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.