2013-03-05 108 views
1

我想在php中製作一個小類,用於集成評分系統,並且我被困在一個可能很簡單的問題上。 我想顯示0到5之間的評級,但投票可以在任何時間間隔,如1到10或1到12. 例如,如果間隔投票是1-12,總得分/總投票將是6我想目前我使用這個限制時間間隔的比例

$rating = number_format(round(($total_score/$total_votes)*2)/2,1); 

所以實際顯示2.5 我怎樣才能讓這個顯示值只有0-5區間?

+0

按照您的術語,我遇到了一些麻煩。你能給一個詳細的評分例子嗎? – mkaatman 2013-03-05 20:40:31

+0

我不明白你的意思。 (雖然我認爲我可以幫助你)你能進一步解釋嗎? – hek2mgl 2013-03-05 20:40:32

+1

那麼,你可以嘗試採取百分比評級,然後將其轉換爲五分之一。例如:6/10 = .6,.6 * 5 = 3. – 2013-03-05 20:41:34

回答

0
$fromminrate = 1; 
$frommaxrate = 12; 
$tominrate = 0; 
$tomaxrate = 5; 
$rating = $tominrate + (
    ((($total_score/$total_votes)-$fromminrate) 
    /($frommaxrate-$fromminrate)) 
    * ($tomaxrate-$tominrate)); 
+0

如果你記住max = best,min = worst,那麼你甚至可以交換那些(所以,最小的0 =最差,最大的-12 =最好),仍然可以正確翻譯。 – Wrikken 2013-03-05 20:46:41

+0

謝謝!這工作,如果你是古怪的,這是我如何使用它https://github.com/ionutvmi/Rating-System/blob/master/rating_system.php – 2013-03-05 21:32:50

0

使用一個簡單的百分比計算是這樣的:

<?php 

$number_of_votes = 10; // real votes 
$max_number_of_votes = 12; // vote range 
$max_display_votes = 5; // display range 

$perc = $max_display_votes * $number_of_votes/$max_number_of_votes; 
$display = intval(round($perc)); // optional, round and convert to int 

echo $display; 

由於投票範圍 - 由它的性質 - 從0開始,你不用擔心下邊框可以簡化您的計算。 )


說明:

的$ number_of_votes(10)都與$ max_number_of_votes(12)如在問題($顯示)的值與$ max_display_votes(5)。在數學:

$number_of_votes/$max_number_of_votes == $display/$max_display_votes; 

或在例如:

10/12 = $display/5; 

可以通過乘以5變換該術語:

10 * 5/12 = $display; 

和這就是我的 '式';)

+0

多數民衆贊成在我的理解你的問題。真的很難理解你的嘗試。請提高您的問題,我會給予更多幫助。順便說一句,你使用一次投票一次得分。有什麼不同?有區別嗎? – hek2mgl 2013-03-05 21:18:40