user2849838 user2849838 - 1 month ago 11
MySQL Question

PHP - Table id the same

How can I put identical columns (sorted by "level" in this case) together? I am making a highscore where I list them by level from my database. I want them to have the same ID if they are the same level.

But I do not want to display the ID on the others. Only the first one.
Here is an example:

ID - Name - Level
1 - John - 5
2 - David - 4
3 - Josh - 3
- Sam - 3
4 - George - 2


So I want to put them together, but if they have the same level, only the first one displays the ID.

I don't want it to look like:

1 - John - 5
2 - David - 4
3 - Josh - 3
3 - Sam - 3
4 - George - 2


Right now, it is just listing everyone, and giving each one a unique ID. Even if they have the same "level".
How can I fix this? Here is my code:

<?php
$sql = mysql_query("SELECT * FROM rookstayers ORDER BY level DESC LIMIT 0, 500");
$id = 1;
while($row = mysql_fetch_array($sql)){
$name = $row['name'];
$level = $row['level'];
$world = $row['world'];
$account = $row['accountstatus'];
$status = $row['onlinestatus'];
$onrow = '';
$typeServ = '';

$player_name = urlencode($name);

if ($status == 1){
$status = 'Online';
$onrow = 'online';
} else {
$status = 'Offline';
$onrow = 'offline';
}

if ($account == 'Premium Account'){
$account = 'Premium';
} else {
$account = 'Free';
}

if ($world == 'Aurora' || $world == 'Aurera'){
$typeServ = 'active';
} else {
$typeServ = '';
}

echo "<tr class=" . $typeServ . ">";
echo "<td>" . $id . "</td>";
echo "<td>" . $name . " <a href='http://www.tibia.com/community/?subtopic=characters&name=" . $player_name . "' target='_blank'><img src='../../img/websites/tibia.png' title='Tibia Profile'></a><a href='http://www.pskonejott.com/otc_display.php?character=" . $player_name . "' target='_blank'><img src='../../img/websites/pskonejott.png' title='Pskonejott'></a>" . "</td>";
echo "<td>" . $level . "</td>";
echo "<td>" . $world . "</td>";
echo "<td>" . $account . "</td>";
echo "<td class=" . $onrow . ">" . $status . "</td>";
echo "</tr>";

$id++;
}
echo "</tbody>";
echo "</table>";
?>

Answer

you can create a temporary variable for the current level and check it to see if the id will be shown on the output or not.

    $id = 1;
     $last_player_lvl = '';
     while($row = mysql_fetch_array($sql)){
      ///....

     echo "<tr class=" . $typeServ . ">";
                         echo "<td>" . ( ($last_player_lvl == $row['level']) ? '' : $id ) . "</td>";
                          echo "<td>" . $name . " <a href='http://www.tibia.com/community/?subtopic=characters&name=" . $player_name . "' target='_blank'><img src='../../img/websites/tibia.png' title='Tibia Profile'></a><a href='http://www.pskonejott.com/otc_display.php?character=" . $player_name . "' target='_blank'><img src='../../img/websites/pskonejott.png' title='Pskonejott'></a>" . "</td>";
                          echo "<td>" . $level . "</td>";
                          echo "<td>" . $world . "</td>";
                          echo "<td>" . $account . "</td>";
                          echo "<td class=" . $onrow . ">" . $status . "</td>";
                        echo "</tr>";
if($last_player_lvl == $row['level']){
    $id = $id;
}else{
    $id++;
}
$last_player_lvl = $row['level'];  
//....