次のような単純なパーセンテージ計算を使用します。
<?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;
投票範囲は、その性質上、ゼロから始まるため、下の境界線について心配する必要はなく、計算を簡素化できます。;)
説明:
問題の値($ display)は$ max_display_votes(5)に関連しているため、$ number_of_votes(10)は$ max_number_of_votes(12)に関連しています。数学では:
$number_of_votes / $max_number_of_votes == $display / $max_display_votes;
または例では:
10 / 12 = $display / 5;
この項は、5を掛けることで変換できます。
10 * 5 / 12 = $display;
そしてそれは私の'式'です;)