PHP/mysql:通过点击排名用户 [英] PHP/mysql:Rank users by clicks

查看:78
本文介绍了PHP/mysql:通过点击排名用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想通过点击对用户进行排名,这是我的代码:

So i want to rank users by clicks here is my code:

<!DOCTYPE html>
<html>
<head>
<title>User ranking</title>
</head>
<body>

<table style="width:100%">
  <tr>
    <th>Rank</th>
    <th>User</th> 
    <th>clicks</th>
  </tr>
  <tr>
    <td></td>
    <td>

    <?php
    $servername = "localhost";
    $username = "...";
    $password = "...";
    $dbname = "...";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT DISTINCT  id, user FROM users";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo $row["user"]. "<br>";
        }
    } else {
        echo "0 results";
    }
    $conn->close();
    ?>
    </td>
    <td></td>
  </tr>

</table>

</body>
</html>

但是这是我的问题:用户"列中有重复的用户,在表中我只想将唯一身份用户计数为用户",并且您注意到我没有在php代码中提及点击次数",因为我没有数据库中没有点击列...

But here is my problem:The "users" column has duplicate users, In the table i want to count only Unique users as "Users" AND you noticed i haven't mention "clicks" in the php code because i don't have clicks column in the database...

这就是我要计算点击次数的方式:正如我所说的,用户"列中有重复的用户,我想计算用户名重复的次数并对其进行排名.

So here is how i want to count clicks: As i said the "users" column has duplicate users i want to count how many times a username has duplicated and rank them.

例如: 用户John X3和Dan X2在数据库中 我想计算John和Dan重复了多少次,因为John是John的3倍,而John排名#1,Dan应该是#2

For example: User John X3 and Dan X2 is in the database i want to count how many times John and Dan duplicated because John is 3 times John is ranked #1 and Dan should be #2

这也是另一个问题: 数据库每24小时重置一次,因此所有数据每24小时消失一次. 如何解决该问题?如何永久显示数据库数据,我的意思是显示数据库数据,然后在24小时后保持其增加...

Also here is another problem: The database resets every 24 hours so all data is gone every 24 hours. How to solve that?how to permanently display database data, I mean display database data and then after 24 hours keep it increasing...

我不知道这是否有用:数据库具有"Id"列,并且不会重置 例如,约翰在数据库中的访问次数是3次,约翰ID 1",约翰ID 2"和约翰ID 3".

I don't know if this is useful or not: the database has "Id" column and it doesn't reset for example 3 times John is in the database, 'John with id 1' 'john with id 2' and 'john with id 3'.

如果使用了JS,没问题. 顺便说一句,我是一个初学者,我尝试这样做.我花了大约7个小时,但每次尝试都失败了.

No problem if JS has been used. Btw i'm a beginner i tried to do that...i spent about 7 hours but every try was failed.

感谢您的回答

推荐答案

这就是我要计算点击次数的方式:正如我所说的,用户"列中有重复的用户,我想计算用户名重复的次数并对其进行排名.

So here is how i want to count clicks: As i said the "users" column has duplicate users i want to count how many times a username has duplicated and rank them.

您要聚合:

select user, count(*) clicks from users group by user order by clicks desc

这篇关于PHP/mysql:通过点击排名用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆