-1

How can I add half star and what will be the calculation for the rating system in php?

Check the following code and modify.

$rating = total rating from server.

I want calculation for $rating value if it is 1.3 - 1.7 it will add half star with color:orange else if 1 or 1.0 - 1.2 and 1.8 - 2.0 then it will add a fa-star with color:orange otherwise it will add a fa-star with color:#ccc.

Thanks for reading this question and for your help.

<?php
    $i;
    $color;

    for($i = 1; $i <= 5; $i++)
    {
        if($i <= $rating){ //$rating = total rating from server
            $color = "color:orange";
            echo "<i class='fa fa-star' style='$color;'></i>";
        } else {        
            $color = "color:#ccc";                 
            echo "<i class='fa fa-star' style='$color;'></i>";
        }
    }
?>
user10972947
  • 51
  • 1
  • 6
  • A similar question and answers already exist in this thread: https://stackoverflow.com/questions/22146839/colour-half-an-icon-using-css – Jim Grant Apr 25 '19 at 08:27
  • 2
    Possible duplicate of [Colour half an icon, using css](https://stackoverflow.com/questions/22146839/colour-half-an-icon-using-css) – Adrita Sharma Apr 25 '19 at 09:11
  • Related: [Laravel - Star rating - Optimization?](https://stackoverflow.com/q/47486060/2943403) – mickmackusa Dec 29 '22 at 20:30

1 Answers1

0

You should use client side rating library to use this functionality. In your scenario, PHP will only handle insert/update votes(ratings) in the database.

You can checkout javascript rating libraries here: https://www.jqueryscript.net/tags.php?/Rating/

sid
  • 365
  • 2
  • 11