Skip to content

Instantly share code, notes, and snippets.

@tiagofrancafernandes
Last active September 2, 2021 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiagofrancafernandes/1c5752ba2ebeb477975c6c016a37fc5c to your computer and use it in GitHub Desktop.
Save tiagofrancafernandes/1c5752ba2ebeb477975c6c016a37fc5c to your computer and use it in GitHub Desktop.
Count ratings in PHP

Count ratings in PHP

How to use

$ratings    = [10,5,7,2];
$max_rating = 10;

//Rating with representation
$rating         = getRatings($ratings, $max_rating, true);

//Rating without representation
$rating         = getRatings($ratings, $max_rating);

//Only representation
$representation = getRatingsRepresentation(4.8, $max_rating);

Examples:

/*
avaliação de 0 à 10
*/
$ratings = [
    11,
    10,
    1,
    7,
    6,
    5,
    5,
    0,
];

$max_rating = 10;
$rating     = getRatings($ratings, $max_rating, true);

print_r($rating);
  • Output
Array
(
    [total_ratings] => 7
    [rating] => 4.8571428571429
    [max_rating] => 10
    [invalid_ratings] => 1
    [representation] => Array
        (
            [0] => Array
                (
                    [type] => full
                )

            [1] => Array
                (
                    [type] => full
                )

            [2] => Array
                (
                    [type] => full
                )

            [3] => Array
                (
                    [type] => full
                )

            [4] => Array
                (
                    [type] => half
                )

            [5] => Array
                (
                    [type] => none
                )

            [6] => Array
                (
                    [type] => none
                )

            [7] => Array
                (
                    [type] => none
                )

            [8] => Array
                (
                    [type] => none
                )

            [9] => Array
                (
                    [type] => none
                )

            [10] => Array
                (
                    [type] => none
                )

        )

)
  • Only representation
$representation = getRatingsRepresentation(4.8, $max_rating);
print_r($representation);
Array
(
    [0] => Array
        (
            [type] => full
        )

    [1] => Array
        (
            [type] => full
        )

    [2] => Array
        (
            [type] => full
        )

    [3] => Array
        (
            [type] => full
        )

    [4] => Array
        (
            [type] => half
        )

    [5] => Array
        (
            [type] => none
        )

    [6] => Array
        (
            [type] => none
        )

    [7] => Array
        (
            [type] => none
        )

    [8] => Array
        (
            [type] => none
        )

    [9] => Array
        (
            [type] => none
        )

    [10] => Array
        (
            [type] => none
        )

)
<?php
/**
* Count ratings
* Author: Tiago França <tiagofranca.com>
* Date: 2021-09-02 14:35:26
* https://gist.github.com/tiagofrancafernandes/1c5752ba2ebeb477975c6c016a37fc5c
*/
function getRatingsRepresentation(float $rating, int $max_rating, array $representation = [])
{
$representation = [
'none' => $representation['none'] ?? 'none',
'half' => $representation['half'] ?? 'half',
'full' => $representation['full'] ?? 'full',
];
for ($i=1; $i <= $max_rating; $i++)
{
if($rating == 0)
$output[$i] = ['type' => $representation['none']];
else if($rating == $max_rating)
$output[$i] = ['type' => $representation['full']];
else if($rating < $max_rating)
{
if($i < $rating)
{
if(($i+1) > $rating)
$output[$i] = ['type' => $representation['half']];
else
$output[$i] = ['type' => $representation['full']];
continue;
}
else
$output[$i] = ['type' => $representation['none']];
continue;
}
}
return $output ?? [];
}
function getRatings(array $ratings, int $max_rating, bool $get_representation = false, array $representation = [])
{
$_ratings = array_filter($ratings, function ($value, $key) use ($max_rating, $count_zero) {
$validation = $value >= 0 && $value <= $max_rating;
if($validation)
return true;
}, ARRAY_FILTER_USE_BOTH);
$sum = array_sum($_ratings);
$count = count($_ratings);
$rating = (float) ($count && $sum) ? $sum/$count : 0;
$invalid_ratings = (count($ratings) - $count);
$output = [
'total_ratings' => $count,
'rating' => $rating,
'max_rating' => $max_rating,
'invalid_ratings' => $invalid_ratings,
'count_zero' => $count_zero,
];
if($get_representation)
$output['representation'] = getRatingsRepresentation($rating, (int) $max_rating, $representation);
return $output;
}
<?php
require_once __DIR__ . '/count-ratings.php';
$max_rating = 10;
$ratings = [ 11, 10, 10, 1, 6, 5, 5, 0, ];
$representation = [
'none' => 'https://raw.githubusercontent.com/TiagoFrancaFernandesOPEN/my-assets/main/star-ratings/star-rating-none.png',
'half' => 'https://raw.githubusercontent.com/TiagoFrancaFernandesOPEN/my-assets/main/star-ratings/star-rating-half.png',
'full' => 'https://raw.githubusercontent.com/TiagoFrancaFernandesOPEN/my-assets/main/star-ratings/star-rating-full.png',
];
$rating_data = getRatings($ratings, $max_rating, true, $representation);
$rating = $rating_data['rating'];
$representation = $rating_data['representation'];// Or getRatingsRepresentation($ratings, $max_rating, $representation);
?>
<div style="background-color: gray; color: white;" title="<?php echo number_format($rating_data['rating'], 2) .'/'. $rating_data['max_rating']; ?>">
<?php
foreach ($representation as $star)
echo '<img src="'. $star['type'] .'" alt="'. $star['type'] . '" style="width: calc(100% / '. $max_rating .');">';
echo "<hr>";
echo number_format($rating_data['rating'], 2) .'/'. $rating_data['max_rating'] .' ('. $rating_data['total_ratings'].' total votes)';
?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment