Answer the question
In order to leave comments, you need to log in
Top users, and displaying the current position in the profile, how to implement it correctly in MYSQL + PHP?
I welcome everyone! I am a beginner and I need to make a Place in the ranking, I did sorting by points. That is, if the user has the most points, he is displayed above all, but I still need to add a type of place. It is also necessary that I display them in the user profile
<table style="margin-left: auto;margin-right: auto; margin-top: 20px;">
<tr><th style="color: black; text-align: center;">Игрок</th><th style="color: black; text-align: center;">Команда</th><th style="color: black;">ROT <i style="cursor: pointer;" title="ROT- Raiting One Tour" class="fas fa-question-circle"></i></th></tr>
<?php
db();
$rait = mysqli_query($db, "SELECT `login`, `rating`, `team` FROM `users` ORDER BY `rating` DESC LIMIT 100");
if(mysqli_num_rows($rait)) {
while ($row = mysqli_fetch_assoc($rait))
echo '<tr><td>'.$row['login'].'</td><td>'.$row['team'].'</td><td>'.$row['raiting'].'</td></tr>';
}
?>
</table>
Answer the question
In order to leave comments, you need to log in
Pure SQL solution - enumerate returned rows via cross join
SELECT
(@row_number:[email protected]_number + 1) AS pos,
`login`, `rating`, `team`
FROM `users`, (SELECT @row_number:=0) AS t
ORDER BY `rating` DESC
LIMIT 100
<table style="margin-left: auto;margin-right: auto; margin-top: 20px;">
<tr><th style="color: black; text-align: center;">Игрок</th><th style="color: black; text-align: center;">Команда</th><th style="color: black;">ROT <i style="cursor: pointer;" title="ROT- Raiting One Tour" class="fas fa-question-circle"></i></th></tr>
<?php
db();
$rait = mysqli_query($db, "SELECT `login`, `rating`, `team` FROM `users` ORDER BY `rating` DESC LIMIT 100");
if(mysqli_num_rows($rait)) {
$position = 0;
while ($row = mysqli_fetch_assoc($rait))
echo '<tr><td>'.(++$position).'. '.$row['login'].'</td><td>'.$row['team'].'</td><td>'.$row['raiting'].'</td></tr>';
}
?>
</table>
SELECT (COUNT(*)+1) as rating_pos FROM users WHERE `rating` > 'рейтинг_текущего_профиля'
place_in_rating | player_name | glasses
SELECT место_в_рейтинге, имя_игрока, очки
FROM название_таблицы
ORDER BY место_в_рейтинге DESC
-- вывод рейтинга
SELECT место_в_рейтинге
FROM название_таблицы
WHERE имя_игрока = 'имя_игрока'
-- вывод места в рейтинге
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question