计分板 PHP 循环

发布于 2024-12-12 03:51:39 字数 918 浏览 0 评论 0原文

我对 php 相当陌生,所以可能我的问题对很多人来说听起来很简单,但这是我的问题。

我在 MySQL 中有一个表,为用户保存记分板。

$connection = mysql_connect('localhost', 'root', '');
$select_db = mysql_select_db('score');
$sql = mysql_query("SELECT * FROM users ORDER BY >score");

function score_table() {
    global $sql;

    if ($sql) {
        $rows_num = mysql_num_rows($sql);
        while ($row = mysql_fetch_array($sql)) {
            for ($i = 0; $i <= $rows_num; $i++) {
                echo $i;
            }
            echo $i.$row['name']." ".$row['score']."<br />\n";
        }
    }
}

我得到的结果是:

123456Player1 3
123456Player2 400
123456Player3 784
123456Player4 1500
123456Player5 1642

所以有 5 名玩家。虽然首先 $i 有 6 个结果,并且它会为每个玩家执行整个循环。

我想要实现的是:

1Player1 3
2Player2 400
3Player3 784
4Player4 1500
5Player5 1642

第一个数字只是位置。所以谁的分数少谁就排在第一位。

I am fairly new to php so probably my question will sound simple for many, but here is my issue.

I have a table in MySQL holding scoreboard for users.

$connection = mysql_connect('localhost', 'root', '');
$select_db = mysql_select_db('score');
$sql = mysql_query("SELECT * FROM users ORDER BY >score");

function score_table() {
    global $sql;

    if ($sql) {
        $rows_num = mysql_num_rows($sql);
        while ($row = mysql_fetch_array($sql)) {
            for ($i = 0; $i <= $rows_num; $i++) {
                echo $i;
            }
            echo $i.$row['name']." ".$row['score']."<br />\n";
        }
    }
}

the result im getting is :

123456Player1 3
123456Player2 400
123456Player3 784
123456Player4 1500
123456Player5 1642

So there is 5 players. Although firstly $i has 6 results and it is going through the entire loop for each player.

What i am trying to achieve is this:

1Player1 3
2Player2 400
3Player3 784
4Player4 1500
5Player5 1642

where first number is simply position. So whoever has less points is on the first place.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转角预定愛 2024-12-19 03:51:39
$connection = mysql_connect('localhost' ,'root', '');
$select_db = mysql_select_db('score');
$sql = mysql_query("SELECT * FROM users ORDER BY >score");

function score_table()
{
  global $sql;
  $i=1;

  if($sql)
  {
    while($row = mysql_fetch_array($sql)) 
    {
      echo $i++ . $row['name'] . " " . $row['score'] . "<br />".PHP_EOL;
    }
  }
}
$connection = mysql_connect('localhost' ,'root', '');
$select_db = mysql_select_db('score');
$sql = mysql_query("SELECT * FROM users ORDER BY >score");

function score_table()
{
  global $sql;
  $i=1;

  if($sql)
  {
    while($row = mysql_fetch_array($sql)) 
    {
      echo $i++ . $row['name'] . " " . $row['score'] . "<br />".PHP_EOL;
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文