Table position

Asked

Viewed 50 times

1

hello, I’d like to know how I can number the table by numbers, example, each would have their indicator as I’m making 1 top 100 players and wanted to number each with the position number

    public function Skywars()
    {
        $stmt = $this->pdo->prepare("SELECT * FROM tskywars INNER JOIN tcommons ON tskywars.id = tcommons.id INNER JOIN permusers ON tcommons.id = permusers.id ORDER BY `solowins` DESC LIMIT 100");
        $stmt->execute();
        if($stmt->rowCount() == 0) { return ""; }
        $fetch = $stmt->fetchAll(\PDO::FETCH_OBJ);
        $return = "";
        foreach ($fetch as $rs)
        {
            $deaths = "".number_format($rs->solodeaths, 0, ',', '.');
            $wins = "".number_format($rs->solowins, 0, ',', '.');
            $kills = "".number_format($rs->solokills, 0, ',', '.');
            $coins = "".number_format($rs->coins, 0, ',', '.');

               $color = "background: ";

    switch($rs->groups){
        case "Diretor": $color."orange;"; break; // se for diretor
        case "Ajudante": $color."yellow;"; break; // se for ajudante
        default : $color."black;";
    }

    // insere a cor

            $return .= "
                         <tr>
                                        <td style=\"padding:10px;\" class=\"position\">1.</td>
                                        <td><img src=\"https://minotar.net/helm/{$rs->name}/24.png\"></td>
                                        <td style=\"padding:10px;\"><span style=\"color:#fff;padding:5px;$color\">{$rs->groups}</span> <span style=\"font-weight:200;\">{$rs->name}</span></td>
                                        <td style=\"padding:10px;\">$wins</td>
                                        <td style=\"padding:10px;\">$kills</td>
                                    </tr>";
        }
        return $return;
    }
  • Hello! You say number the line <td style=\"padding:10px;\" class=\"position\">1.</td>. Sequence? type first line 1., second line: 1, third line: 3. This?

  • this, the first table would be 1, the second would be 2 and so on

2 answers

1

Just use the post-increment operation.

Note: Increment/Decrease operators only affect numbers and strings. Arrays, objects and resources are unaffected. Decrement NULL does not generate effects, but incrementing results in 1.

See the variable $position added in your code and comment.

public function Skywars()
    {
        $stmt = $this->pdo->prepare("SELECT * FROM tskywars INNER JOIN tcommons ON tskywars.id = tcommons.id INNER JOIN permusers ON tcommons.id = permusers.id ORDER BY `solowins` DESC LIMIT 100");
        $stmt->execute();
        if($stmt->rowCount() == 0) { return ""; }
        $fetch = $stmt->fetchAll(\PDO::FETCH_OBJ);
        $return = "";

        // fora do foreach - Valor para o primeiro item
        $posicao = 1;

        foreach ($fetch as $rs)
        {
            $deaths = "".number_format($rs->solodeaths, 0, ',', '.');
            $wins = "".number_format($rs->solowins, 0, ',', '.');
            $kills = "".number_format($rs->solokills, 0, ',', '.');
            $coins = "".number_format($rs->coins, 0, ',', '.');

               $color = "background: ";

    switch($rs->groups){
        case "Diretor": $color."orange;"; break; // se for diretor
        case "Ajudante": $color."yellow;"; break; // se for ajudante
        default : $color."black;";
    }

    // insere a cor

    $return .= "
                 <tr>
                    <td style=\"padding:10px;\" class=\"position\"> ". $posicao ."</td>
                    <td><img src=\"https://minotar.net/helm/{$rs->name}/24.png\"></td>
                    <td style=\"padding:10px;\"><span style=\"color:#fff;padding:5px;$color\">{$rs->groups}</span> <span style=\"font-weight:200;\">{$rs->name}</span></td>
                    <td style=\"padding:10px;\">$wins</td>
                    <td style=\"padding:10px;\">$kills</td>
                </tr>";

//Pós-incremento - Retorna $posicao, e então incrementa $posicao em um.
$posicao++;

}
return $return;
}

1


You can make the foreach return also the key(in your case will be the index) of the current element:

foreach ($fetch as $key => $rs)

After that just use:

<td style='padding:10px' class='position'>{$key}.</td>

Remember that if you have 100 elements, it will go from 0-99, if you want it to go from 1-100 just add 1:

$posicao = $key + 1;
// ...
<td style='padding:10px' class='position'>{$posicao}.</td>

Browser other questions tagged

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