I have tried multiple ways to get this to work, but all it shows are 5 grey stars. Of the 25 search results I get, I was expecting 1 result to have 5 gold stars, 1 to have 3 gold stars/2 grey stars with the other results displaying all 5 grey stars.
Here’s is my code to date (I replaced a lot of the redundant code with etc. etc. to save space:
Also, I am aware that I should use mysqli and the risk of injection. Ill change that later as it is not on a production site yet.
<?php
$id = $_GET['id'];
$link = mysql_connect("localhost", "****", "****");
mysql_select_db("****", $link);
$query = "SELECT AVG(`rating`)
FROM (
SELECT `rating` FROM `comments` WHERE `is_approved` = '1' AND `rating` != '0' AND
`product_id` = '$id' AND reply_to = '0')
AS `average`
";
$result = $query;
$average = round($average, 1);
if ($average == 1.0) {
echo '<img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full-grey.png"><img src="images/stars/google-star_full-grey.png"><img src="images/stars/google-star_full-grey.png"><img src="images/stars/google-star_full-grey.png">';
} elseif ($average == 1.1) {
echo '<img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full-grey.png"><img src="images/stars/google-star_full-grey.png"><img src="images/stars/google-star_full-grey.png"><img src="images/stars/google-star_full-grey.png">';
} elseif ($average == 1.2) {
echo '<img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full-grey.png"><img src="images/stars/google-star_full-grey.png"><img src="images/stars/google-star_full-grey.png"><img src="images/stars/google-star_full-grey.png">';
} elseif ($average == 1.3) {
echo '<img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full-grey.png"><img src="images/stars/google-star_full-grey.png"><img src="images/stars/google-star_full-grey.png"><img src="images/stars/google-star_full-grey.png">';
}
etc. etc. etc.
elseif ($average == 4.7) {
echo '<img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full-half.png">';
} elseif ($average == 4.8) {
echo '<img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full.png">';
} elseif ($average == 4.9) {
echo '<img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full.png">';
} elseif ($average == 5.0) {
echo '<img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full.png"><img src="images/stars/google-star_full.png">';
} else {
echo '<img src="images/stars/google-star_full-grey.png"><img src="images/stars/google-star_full-grey.png"><img src="images/stars/google-star_full-grey.png"><img src="images/stars/google-star_full-grey.png"><img src="images/stars/google-star_full-grey.png">';
}
?>
You need to sign in to view this answers
Leave feedback about this