PHP/mysql:排名用户

Posted

技术标签:

【中文标题】PHP/mysql:排名用户【英文标题】:PHP/mysql:Rank users 【发布时间】:2019-12-06 22:40:21 【问题描述】:

我有这张表,它显示来自 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

感谢回答

【问题讨论】:

Haven't you already asked something similar? @FunkFortyNiner 不,完全不同,是的,标题相似但不一样 好的。很高兴看到你有解决方案,干杯 【参考方案1】:
    将排名部分添加到表中
<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>

【讨论】:

最后一件事如何在每行之间添加行?不能使用 CSS 添加行,因为它的 PHP 行现在是表格行 编辑:更改 to
这就是我的意思 :)
【参考方案2】:

使用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>

【讨论】:

我已经对用户进行了排名,我想创建另一个表格列来显示他们的排名...

以上是关于PHP/mysql:排名用户的主要内容,如果未能解决你的问题,请参考以下文章

sql 从两列PHP MySql中查找排名

根据结果​​序列对 PHP MySQL 搜索结果进行排名

PHP+MYSQL查询结果上加一个排名序号且分页不间断

php mysql用户登录显示用户存在

php+MySQL实战案例新增用户

php+mysql,将用户session信息存到数据库,如何及时删掉登陆超时的用户信息呢?