为了账号安全,请及时绑定邮箱和手机立即绑定

PHP/mysql:排名用户

PHP/mysql:排名用户

PHP
开满天机 2022-06-11 17:00:34
我有这张表,它显示来自 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    clicksuser1    3user2    2user3    1但我希望输出为:rank    users    clicks1       user1    32       user2    23       user3    1
查看完整描述

2 回答

?
POPMUISE

TA贡献1765条经验 获得超5个赞

将排名部分添加到表中

<tr>

    <th>Rank</th> 

    <th>User</th> 

    <th>clicks</th>

</tr>

为每个结果添加排名

<td>

    <?php include 'conn.php';

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

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

    $rank = 1;

    if ($result->num_rows > 0) {

        // output data of each row

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

            echo $rank. "<br>";

            $rank++;

        }

    }

    ?>

</td>

完整代码


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

  <tr>

    <th>Rank</th> 

    <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);

    $rank = 1;

    if ($result->num_rows > 0) {

        // output data of each row

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

            echo $rank. "<br>";

            $rank++;

        }

    }

    ?>

    </td>

    <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>";

        }

    }

    ?>

    </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>";

      }

  }

  $conn->close();

    ?></td>

  </tr>

</table>


查看完整回答
反对 回复 2022-06-11
?
手掌心

TA贡献1942条经验 获得超3个赞

使用 javascript:


<script type="text/javascript">

var nb = document.getElementsByClassName("number");

for (var i = 0; i < nb.length; i++) {

    nb[i].innerHTML = (i+1)+".";

 }

</script>



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

<tr>


    <th>User</th>

    <th>clicks</th>

</tr>

<tr>


    <td class="number">


        <?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>


查看完整回答
反对 回复 2022-06-11
  • 2 回答
  • 0 关注
  • 92 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信