0

I have managed to output the average rating in a number format, but i would like to output it by using the star method as well.

This is my code so far that outputs the average rating by using numbers;

<?php

$sql = "
    SELECT round(avg(`rate`),2) AS `average_rate`, count(`rate`) AS `num_of_rating`
    FROM tbl_rating 
    WHERE hotel_id = '$id' ";
$query = mysqli_query($con,$sql);
$rating = mysqli_fetch_assoc($query);
?>

<div>
    <h4 style="color: #1478FF; font-size:30px; font-family:  " >    <?php echo $rating['average_rate'];  ?> Avergae rating </h4>
</div>

For instance;Stars

Dez
  • 5,702
  • 8
  • 42
  • 51
amir
  • 11
  • 7
  • Your code is vulnerable to [**SQL injection attacks**](https://en.wikipedia.org/wiki/SQL_injection). You should use [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Apr 25 '17 at 15:19
  • Do you have a star sprite or Icon? The simplest way to display the stars is to add a loop after you output the average rating from 1 to Floor(avg_rate) and then display a half star if the fractional part of the rating is greater than 0.49. I'm an old ColdFusion guy, so don't ask me to write the PHP code for this :) –  Apr 25 '17 at 15:22
  • @RusselMadere i didn't understand u – amir Apr 25 '17 at 15:25
  • Here are the steps you need to perform. - Calculate the Floor of the average rating (convert it to an integer without rounding). - Loop from 1 to that value. - Each time through the loop, display a star icon. - After the loop, if the remainder of the average rating is greater than 0.499999 display a half star icon. –  Apr 25 '17 at 15:31
  • @RusselMadere Can you please do the coding part for me? The explanation you gave me is quite complex... – amir Apr 26 '17 at 13:38
  • @amir, I will try. As I stated, I do not know php, so I will be googleing everything. –  Apr 26 '17 at 15:23
  • @RusselMadere i appreciate it. Thanks – amir Apr 26 '17 at 15:27

1 Answers1

0

Please give this a try:

<?php

$sql = "
    SELECT round(avg(`rate`),2) AS `average_rate`, count(`rate`) AS `num_of_rating`
    FROM tbl_rating 
    WHERE hotel_id = '$id' ";
$query = mysqli_query($con,$sql);
$rating = mysqli_fetch_assoc($query);
?>

<div>
    <h4 style="color: #1478FF; font-size:30px; font-family:  " >    <?php echo $rating['average_rate'];  ?> Average rating </h4>
<?php
    $limit = floor($rating['average_rate']);
    $remainder = $rating['average_rate'] % 1.0;

    for( $i = 0; $i<$limit; $i++ ) {
        echo ("<img src=\"\\images\\fullstar.jpg\" />");
    }

    if ($remainder >= 0.5) echo ("<img src=\"\\images\\halfstar.jpg\" />");
?>
</div>

I'm not sure this will work. I have no way of testing the code, I'm a C# and ColdFusion developer. This is what a few minutes on Google gave me.

To make this work, you need images of a full star called fullstar.jpg and of a half start called halfstar.jpg. They both need to be in a folder names images in your project root.

  • What is the rating that should be displayed? Check the value of $limit. if it is not the correct number, let me know. –  Apr 26 '17 at 16:26
  • It's working but it's not displaying the half star. IF its 4 stars its working, but if it's like 3.52 it just displays 3 stars... – amir Apr 26 '17 at 18:14
  • @amir, the key to solving that problem is to look at the value of $remainder and make sure it is calculating properly you could replace the line with $remainder = $rating['average_rate'] - $limit; –  Apr 26 '17 at 18:21
  • Okay its working fine now. However, i have one more question. Is it possible to display the rest of the stars but empty? For instance, if its 3.5 rating, it will display the 3.5 stars and the rest of the stars empty. Do you get what i mean? – amir Apr 26 '17 at 18:47
  • @amir, yes you can. you will need to add another loop. To calculate that index, you need to use this code - $emptyStarStart = floor($rating['average_rate'] + 0.5); and loop like for( $emptyStarStart; $emptyStarStart<5; $emptyStarStart++ ) {... or something similar. –  Apr 27 '17 at 16:34
  • @amir, if this is now working for you, please accept the answer. –  Apr 28 '17 at 16:28