簡體   English   中英

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

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

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

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

$total_votes = 43

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

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

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

需要像這樣:

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

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

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

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

問候

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM