如何計算星級評分系統的平均值?

[英]How to calculate average on star rating system?


I am developing a star rating system with 1-5 stars. In my database I am saving them like this:

我正在開發一個1-5星級的星級評分系統。在我的數據庫中,我將它們保存為:

$stars_1 = 1;
$stars_2 = 6;
$stars_3 = 3;
$stars_4 = 11;
$stars_5 = 22;

$total_votes = 43

When a user votes using for example 3 stars I update stars_3 with 1 and total_votes with 1. Then I need to calculate the average rating (stars).

當用戶使用例如3星的投票時,我將stars_3更新為1,將total_votes更新為1.然后我需要計算平均評級(星級)。

I do it like this right now but I is not working (result seems wrong):

我現在這樣做,但我沒有工作(結果似乎錯了):

(($stars_1 + $stars_2 + $stars_3 + $stars_4 + $stars_4) / $total_votes);

2 个解决方案

#1


13  

Needs to be like this:

需要像這樣:

($stars_1 + $stars_2 * 2 + $stars_3 * 3 + $stars_4 * 4 + $stars_5 * 5) / $total_votes;

#2


2  

You need to multiply the number of stars with the actual rating. Like

您需要將星數乘以實際評級。喜歡

$points_stars_2 = $stars_2 * 2  
...  
$points_stars_5 = $stars_5 * 5 

And then you add them all to one variable like in your code, and then divide it by $total_votes.

然后將它們全部添加到代碼中的一個變量中,然后將其除以$ total_votes。

Regards

問候


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/03/19/75638e465b2d7d97983f07219d187d92.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com