How to designate a number for each placement

Asked

Viewed 55 times

2

I am making a page where shows the rankings of users with more X and Y, everything is shown inside the site in a table where there is the name of each user and the amount of X that it possesses, but I do not know how to put for example: 1º or 1. Robson 100 (Diamonds), I mean I can’t put the first column with the placements, to select the users and to show how much each one has I managed, just need to enter the placements.

<?php
    # conectare la base de datos
    require ('../../global.php');

    $action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
    if($action == 'ajax'){
        require ('../../hk/pagination.php'); //incluir el archivo de paginación
        //las variables de paginación
        $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
        $per_page = 10; //la cantidad de registros que desea mostrar
        $adjacents  = 4; //brecha entre páginas después de varios adyacentes
        $offset = ($page - 1) * $per_page;
        //Cuenta el número total de filas de la tabla*/
        $count_query   = $link->query("SELECT count(*) AS numrows FROM usuarios ");
        if ($row= mysqli_fetch_array($count_query)){$numrows = $row['numrows'];}
        $total_pages = ceil($numrows/$per_page);
        $reload = 'rank.php';
        //consulta principal para recuperar los datos
        $query = $link->query("SELECT * FROM usuarios  order by diamantes DESC LIMIT $offset,$per_page");

        if ($numrows>0){
            ?>
<table class="table table-striped table-hover ">
              <thead>
                <tr>
                              <th>Colocação</th>
                              <th><?php echo $lang[27]; ?></th>
                              <th>Diamantes</th>
                </tr>
            </thead>
            <tbody>
            <?php
            while($row = mysqli_fetch_array($query)){
                ?>
                <tr>
                              <td>NESSA LINHA NO ANTIGO CODIGO (De onde copiei) Ficaria o ID do usuário, porém gostaria de por a colocação</td>
                              <td><?php echo "$row[username]"; ?></td>
                              <td><?php echo "$row[diamantes]"; ?></td>
                </tr>
                <?php
            }
            ?>
            </tbody>
        </table>
        <div class="table-pagination pull-right">
            <?php echo paginate($reload, $page, $total_pages, $adjacents);?>
        </div>

            <?php

        } else {
            ?>
            <div class="alert alert-warning alert-dismissable">
              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
              <?php echo $lang[195]; ?>
            </div>
            <?php
        }
    }
?>

1 answer

2


Create a variable before the while to increment in the loop.

For example:

<?php
$x = 1;
while($row = mysqli_fetch_array($query)){
    ?>
    <tr>
                  <td><?php echo $x.'&ordm;'; ?></td>
                  <td><?php echo "$row[username]"; ?></td>
                  <td><?php echo "$row[diamantes]"; ?></td>
    </tr>
    <?php
   $x++;
}
    ?>

Every turn in the while, the $x is increased with +1, showing 1st, 2nd, 3rd and so on.

  • 1

    It worked fine! Thank you! D

  • You’re welcome, mate! D

Browser other questions tagged

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