简体   繁体   English

使用PHP的数学,评分在1-5之间

[英]Maths using PHP, coming up with a rating between 1 to 5

I need a rating system for my app, what happens is that a user can rate a thread 1 to 5. The calculation i was going to use was shown below: 我需要为我的应用程序提供一个评分系统,结果是用户可以将线程1评分为5。我要使用的计算如下所示:

ID        UID        TID        Rating
--------------------------------------
1         1          37         5
2         4          37         5
3         8          37         5
4         22         37         5
5         2          37         5

This is a sample table, as you can see the way i did it was, 这是一个示例表,您可以看到我的处理方式,

   r1        r2        r3        r4        r5
(0 x 1) + (0 x 2) + (0 x 3) + (0 x 4) + (5 x 5)
----------------------------------------------- = 5
                    5

There is no user (UID) with a rating of 1 (r1) so you set r1 = (0 x 1) but there are users (UID) with a rating of 5 (r5) so you set r5 = (5 x 5) and the rest r2, r3, r4 are set to (0) there are no ratings for those. 没有用户(UID)的评级为1(r1),因此您将r1 =(0 x 1)设置为,但有用户(UID)的评级为5(r5),因此将r5 =(5 x 5)设置为其余的r2,r3,r4设置为(0),则没有评级。 Hope you get it, if not ill explain more. 希望您能理解,即使没有生病也请多解释。

so i get a rating of 5 star. 所以我得到5星评级。

But my problem is if 100 different users lets say all rate the thread 5 you will get a rating of 5 using the formula given, also if 5 different users rated another thread 5 you would get a rating of 5 also. 但是我的问题是,如果100个不同的用户说都对线程5进行评分,则使用给定的公式将获得5的评分,如果5个不同的用户对另一个线程5进行评分,您也将获得5的评分。 But i don't want these results as both ratings for each thread will hit the top. 但是我不希望出现这些结果,因为每个线程的两个等级都将达到最高。 I know i could select in sql to order the threads by number of users who have rated the thread so the 100 different users will go top this works, but the threads that had 5 users who rated 5 will be second. 我知道我可以在sql中选择按已对该线程进行评分的用户数来排序该线程,以便100个不同的用户将排在首位,但具有5个用户的5个评分为5的线程将排名第二。

Is there another way to rate theses threads taking into account how many users have rated each thread. 考虑到有多少用户对每个线程进行了评分,是否还有另一种方法可以对这些线程进行评分。

I hope you understand this, my question? 我希望你明白这一点,我的问题吗? if not ill edit. 如果没病,请编辑。

I also need to generate a php script that calculates this in my select statement when i retrieve the rating, but ill ask another question when this is solved. 我还需要生成一个php脚本,当我检索评分时会在我的select语句中对此进行计算,但是当此问题解决时,我会提出另一个问题。 Thanks. 谢谢。

I saw that you mentioned SELECT statements, so I assume you meant the SQL statements required. 我看到您提到了SELECT语句,所以我假设您的意思是所需的SQL语句。

What you asked for can be done purely using SQL itself, using the below line. 您要求的内容可以完全使用SQL本身完成,只需使用以下代码行。

SELECT TID, AVG(RATING)  FROM ratings GROUP BY TID ORDER BY AVG(RATING) DESC, SUM(RATING) DESC;

Here is the data I used for testing. 这是我用于测试的数据。

CREATE TABLE ratings(ID INT,UID INT,TID INT,RATING INT);

INSERT INTO ratings VALUES (1,1,37,5);
INSERT INTO ratings VALUES (2,4,37,5);
INSERT INTO ratings VALUES (3,8,37,5);
INSERT INTO ratings VALUES (4,22,37,5);
INSERT INTO ratings VALUES (5,2,37,5);
INSERT INTO ratings VALUES ( 6,1,12,5);
INSERT INTO ratings VALUES ( 7,4,12,5);

SELECT TID, AVG(RATING)  FROM ratings GROUP BY TID ORDER BY AVG(RATING) DESC, SUM(RATING) DESC;

The output I got 我得到的输出

TID AVG(RATING)
------------------
37  5
12  5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM