0

So I have a function that takes a number and outputs it as a placement. How do I go about making the function take in consideration ties. I have a ranking database and this php code echos out rankings on a table. right now it ranks but it doesn't consider ties. How do i go about doing this

<?php

function addOrdinalNumberSuffix($num) {
if (!in_array(($num % 100),array(11,12,13))){
  switch ($num % 10) {
    // Handle 1st, 2nd, 3rd
    case 1:  return $num.'st';
    case 2:  return $num.'nd';
    case 3:  return $num.'rd';
  }
}
return $num.'th';
  }

?>


<?php

$link = mysqli_connect("localhost", "citricide", "321213123Lol", "juneausmashbros");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT * FROM rankings ORDER BY points DESC";
$result = mysqli_query($link, $query);

echo '<article class="content grid_6 push_3">';
echo '<h1>';
echo 'Project M Summer Ranbat Rankings';
echo '</h1>';
echo '<section>';
echo '<center>';
echo '<table style="width:400px" class="rankslist">';
echo '<tr>';
echo  '<th width="15%"><b>Rank</b></th>';
echo '<th width="45%"><b>Name</b></th>';
echo '<th width="45%"><b>Alias</b></th>';
echo  '<th width="15%"><b>Points</b></th>';
echo '</tr>';
$ass = 0;
while($row =  $result->fetch_array()) {
    $ass++;
    echo '<tr>';
    if ($ass == 1) {
        echo ' <center><td><B><font color=#FFD700>';
    } else if ($ass == 2) {
        echo ' <center><td><B><font color=#CCCCCC>';
    } else if ($ass == 3) {
        echo ' <center><td><B><font color=#cd7f32>';
    } else {
        echo '<td>';
    }
    echo addOrdinalNumberSuffix($ass);
    echo ' </font></B</td></center>';
    echo ' <td>'.$row['name'].'</td>';
    echo  '<td>'.$row['alias'].'</td>' ;
    echo  '<td>'.$row['points'].'</td>';
    echo '</tr>';
}
echo '</table>';
echo '</center>';
echo '</section>';
echo '</article>';
?>     

1 Answer 1

0

It seems like you need additional variables to track the rank and the previous value. By doing so, you can handle ties.

For example:

$ass = 0; // current record count
$rank = 0; // rank
$last_points = NULL; // variable to store last ranked value
while($row =  $result->fetch_array()) {
    $ass++;
    // check if value changes and reset rank if it does
    if ($row['points'] !== $last_points) {
        $rank = $ass;
        $last_points = $row['points'];
    }
    echo '<tr>';
    if ($rank == 1) {
        echo ' <center><td><B><font color=#FFD700>';
    } else if ($rank == 2) {
        echo ' <center><td><B><font color=#CCCCCC>';
    } else if ($rank == 3) {
        echo ' <center><td><B><font color=#cd7f32>';
    } else {
        echo '<td>';
    }
    echo addOrdinalNumberSuffix($rank);
    echo ' </font></B</td></center>';
    echo ' <td>'.$row['name'].'</td>';
    echo  '<td>'.$row['alias'].'</td>' ;
    echo  '<td>'.$row['points'].'</td>';
    echo '</tr>';
}

So say your points looked like this:

100
100
90
80

The $rank value would be:

1
1
3
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.