Search code examples
phprating

constrain proportions between intervals


i'm trying to make a small class in php that would be used to integrate a rating system and i'm stuck on a little probably simple problem. i want to show the rating between 0 and 5 but the votes can be in any interval like 1 to 10 or 1 to 12. for example if interval was of votes was 1-12 and total score/total votes would be 6 i want to actually display 2.5 i'm currently using this

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

so how can i make this to show values only 0-5 interval ?


Solution

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