PHP/mysql:排名用户

我有这张表,它显示来自 mysql 的数据并通过大多数点击对它们进行排序,但我没有 #1 或 #2 等等....这里是代码:


<table align="center" style="width:50%">

  <tr>


    <th>User</th> 

    <th>clicks</th>

  </tr>

  <tr>


    <td>


    <?php include 'conn.php';

    $sql = "select user, count(*) duplicates from users group by user order by duplicates desc";

    $result = $conn->query($sql);


    if ($result->num_rows > 0) {

        // output data of each row

        while($row = $result->fetch_assoc()) {

            echo $row["user"]. "<br>";

        }

    } else {


    }


    ?>

    </td>

    <td>

    <?php

  include 'conn.php';

  $sql = "select user, count(*) AS duplicates from users group by user order by duplicates desc";

  $result = $conn->query($sql);


  if ($result->num_rows > 0) {

      // output data of each row

      while($row = $result->fetch_assoc()) {

          echo $row["duplicates"]. "<br>";

      }

  } else {


  }


  $conn->close();

    ?></td>

  </tr>


</table>

输出在表格中是这样的:


users    clicks

user1    3

user2    2

user3    1

但我希望输出为:


rank    users    clicks

1       user1    3

2       user2    2

3       user3    1


开满天机
浏览 94回答 2
2回答

POPMUISE

将排名部分添加到表中<tr>&nbsp; &nbsp; <th>Rank</th>&nbsp;&nbsp; &nbsp; <th>User</th>&nbsp;&nbsp; &nbsp; <th>clicks</th></tr>为每个结果添加排名<td>&nbsp; &nbsp; <?php include 'conn.php';&nbsp; &nbsp; $sql = "select user, count(*) duplicates from users group by user order by duplicates desc";&nbsp; &nbsp; $result = $conn->query($sql);&nbsp; &nbsp; $rank = 1;&nbsp; &nbsp; if ($result->num_rows > 0) {&nbsp; &nbsp; &nbsp; &nbsp; // output data of each row&nbsp; &nbsp; &nbsp; &nbsp; while($row = $result->fetch_assoc()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $rank. "<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rank++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; ?></td>完整代码<table align="center" style="width:50%">&nbsp; <tr>&nbsp; &nbsp; <th>Rank</th>&nbsp;&nbsp; &nbsp; <th>User</th>&nbsp;&nbsp; &nbsp; <th>clicks</th>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; <td>&nbsp; &nbsp; <?php include 'conn.php';&nbsp; &nbsp; $sql = "select user, count(*) duplicates from users group by user order by duplicates desc";&nbsp; &nbsp; $result = $conn->query($sql);&nbsp; &nbsp; $rank = 1;&nbsp; &nbsp; if ($result->num_rows > 0) {&nbsp; &nbsp; &nbsp; &nbsp; // output data of each row&nbsp; &nbsp; &nbsp; &nbsp; while($row = $result->fetch_assoc()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $rank. "<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rank++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; ?>&nbsp; &nbsp; </td>&nbsp; &nbsp; <td>&nbsp; &nbsp; <?php include 'conn.php';&nbsp; &nbsp; $sql = "select user, count(*) duplicates from users group by user order by duplicates desc";&nbsp; &nbsp; $result = $conn->query($sql);&nbsp; &nbsp; if ($result->num_rows > 0) {&nbsp; &nbsp; &nbsp; &nbsp; // output data of each row&nbsp; &nbsp; &nbsp; &nbsp; while($row = $result->fetch_assoc()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $row["user"]. "<br>";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; ?>&nbsp; &nbsp; </td>&nbsp; &nbsp; <td>&nbsp; &nbsp; <?php&nbsp; include 'conn.php';&nbsp; $sql = "select user, count(*) AS duplicates from users group by user order by duplicates desc";&nbsp; $result = $conn->query($sql);&nbsp; if ($result->num_rows > 0) {&nbsp; &nbsp; &nbsp; // output data of each row&nbsp; &nbsp; &nbsp; while($row = $result->fetch_assoc()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $row["duplicates"]. "<br>";&nbsp; &nbsp; &nbsp; }&nbsp; }&nbsp; $conn->close();&nbsp; &nbsp; ?></td>&nbsp; </tr></table>

手掌心

使用 javascript:<script type="text/javascript">var nb = document.getElementsByClassName("number");for (var i = 0; i < nb.length; i++) {&nbsp; &nbsp; nb[i].innerHTML = (i+1)+".";&nbsp;}</script><table align="center" style="width:50%"><tr>&nbsp; &nbsp; <th>User</th>&nbsp; &nbsp; <th>clicks</th></tr><tr>&nbsp; &nbsp; <td class="number">&nbsp; &nbsp; &nbsp; &nbsp; <?php include 'conn.php';&nbsp; &nbsp; &nbsp; &nbsp; $sql = "select user, count(*) duplicates from users group by user order by&nbsp;&nbsp; &nbsp; duplicates desc";&nbsp; &nbsp; &nbsp; &nbsp; $result = $conn->query($sql);&nbsp; &nbsp; &nbsp; &nbsp; if ($result->num_rows > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // output data of each row&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($row = $result->fetch_assoc()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $row["user"]. "<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; </td>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; include 'conn.php';&nbsp; &nbsp; &nbsp; &nbsp; $sql = "select user, count(*) AS duplicates from users group by user order by&nbsp;&nbsp; &nbsp;duplicates desc";&nbsp; &nbsp; &nbsp; &nbsp; $result = $conn->query($sql);&nbsp; &nbsp; &nbsp; &nbsp; if ($result->num_rows > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // output data of each row&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($row = $result->fetch_assoc()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $row["duplicates"]. "<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $conn->close();&nbsp; &nbsp; &nbsp; &nbsp; ?></td></tr></table>
打开App,查看更多内容
随时随地看视频慕课网APP