Classification system

Asked

Viewed 140 times

3

I’d like to bring up the positions. I have no idea how to do it. Below I’ll be giving a demo and my code

inserir a descrição da imagem aqui

    <?php
   include("./configs/dados.php");
$ranking = "SELECT * FROM usuarios ORDER BY pontos_total  Desc LIMIT 3,10";
$limiteranking = mysql_query("$ranking");
while ($ranking = mysql_fetch_array($limiteranking)) {  
$nomeran = $ranking['login'];
$pontosran = $ranking['pontos_total'];
?>      




    <tr style="background:#d3a298">
      <th scope="row"><center>4º</th></center>
      <td style="color:#383838"><?php echo $nomeran; ?></td>
      <td style="color:#383838"><?php echo $pontosran; ?></td>
    </tr>

<?php } ?>

*Code only four down. As you can see, I would like it to appear 4th , 5th , 6th

2 answers

2


Do so:

<?php
include("./configs/dados.php");
$posicao = 4;
$ranking = "SELECT * FROM usuarios ORDER BY pontos_total  Desc LIMIT 3,10";
$limiteranking = mysql_query("$ranking");
while ($ranking = mysql_fetch_array($limiteranking)) {  
$nomeran = $ranking['login'];
$pontosran = $ranking['pontos_total'];
?>      




    <tr style="background:#d3a298">
      <th scope="row"><center><?php echo $posicao;?>º</th></center>
      <td style="color:#383838"><?php echo $nomeran; ?></td>
      <td style="color:#383838"><?php echo $pontosran; ?></td>
    </tr>

<?php $posicao++; } ?>

Explaining the code:

It creates a variable with the value of 4 which is the first position. Whenever he makes a new loop, ie when he picks up a new record he adds +1 so always leaving the current position of the user in the ranking.

Note: In case you want it to appear as 1st, 2nd, 3rd... Change the $posicao = 4; for $posicao = 1;

  • It worked perfectly buddy. But a doubt: the $position++ means it will sum 1 qnd make the loop?

  • @VME Yes. Every time you loop, that is, list one more user in the ranking, it adds +1 in $position, thus becoming dynamic.

2

Create a variable $i out of the loop by storing how many times the block was iterated (repeated). At each iteration, you add a unit to that variable:

<?php
    include("./configs/dados.php");
    $ranking = "SELECT * FROM usuarios ORDER BY pontos_total  Desc LIMIT 3,10";
    $limiteranking = mysql_query("$ranking");

    $i = 0;
    while ($ranking = mysql_fetch_array($limiteranking)) {  
        $i = $i + 1;

        $nomeran = $ranking['login'];
        $pontosran = $ranking['pontos_total'];
?>      

        <tr style="background:#d3a298">
          <th scope="row"><center><?php echo $i; ?>º</th></center>
          <td style="color:#383838"><?php echo $nomeran; ?></td>
          <td style="color:#383838"><?php echo $pontosran; ?></td>
        </tr>

<?php
    }
?>
  • It worked too. It was simple and I hadn’t thought of it. Thank you

Browser other questions tagged

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