How to build a leaderboard?

Asked

Viewed 57 times

0

I need the help of friends.

I created an ordered table of teams from highest to lowest score.

I wish there was a qualifying order with the 1st, 2nd, 3rd, etc... on the left side of the teams, according to the scores.

Like, the highest score in 1st, second in 2nd, third in 3rd and so on.

But I have no idea how to do it.

But as there are changes in the scores during the championship, the positions alternate.

If anyone can give me an idea or even where I can figure out how to do it, I would be most grateful!!!

Below put the aquivo I’m using.

<table>
   <tr>
     <td align="center" width="120">Nome</td>
     <td align="center" width="120">Time</td>
     <td align="center" width="120">Pontuações</td>         
   </tr>

   <?php
      $sql = $db->query("SELECT * FROM cadastro ORDER BY pontos DESC");
      foreach ($sql as $res){
      echo '<tr style="border-bottom: 1px solid black;">

         <td width="350" align="left" style="font-size: 0.8em; font-weight: bold;">'.$res["name"].'</td>
         <td width="150" align="center"><img style="height:auto; width:50%; max-width:150px;border-radius: 10px;" src="'.$res["end_t_cartola"].'/'.$res["t_cartola"].'"/></td>
         <td align="center">'.$res["pontos"].'</td>
     </tr>';
     }

    ?>
</table>

At the moment the table looks like this:

inserir a descrição da imagem aqui

But I’d like to keep it that way:

inserir a descrição da imagem aqui

I believe that I will have to create a new taela, but I have no idea how to make the two work together.

HELP friend...rsrs...

Thank you for your attention!

1 answer

2


Declare an integer variable before the foreach, and inside it increment it. Then use the variable in the html code next to each printed line. Example:

    <table>
       <tr>
         <td align="center" width="120">Nome</td>
         <td align="center" width="120">Time</td>
         <td align="center" width="120">Pontuações</td>         
       </tr>
    
       <?php
$order = 0;
          $sql = $db->query("SELECT * FROM cadastro ORDER BY pontos DESC");
          foreach ($sql as $res){
          order++;
          echo '<tr style="border-bottom: 1px solid black;">

           <td width="350" align="left" style="font-size: 0.8em; font-weight: 
          bold;">'.$order.'</td>

             <td width="350" align="left" style="font-size: 0.8em; font- 
           weight: bold;">'.$res["name"].'</td>

             <td width="150" align="center"><img style="height:auto; 
  width:50%; max-width:150px;border-radius: 10px;" src="'.$res["end_t_cartola"].'/'.$res["t_cartola"].'"/></td>

             <td align="center">'.$res["pontos"].'</td>
         </tr>';
         }
    
        ?>
    </table>
  • duly corrected.

  • Thank you very much Alexandre Guerreiro, it worked perfect!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.