Do it like the pros...
This is a piece of a much larger rating system that I built, so you will need to tweak.
It is a simple switch that attaches a 'star' rating
to an average of all ratings in a mysql database.









Code: Select all
<?php
function getStars($num){
switch($num){
case ($num > 4.75):
$image="stars-5-0.gif";
break;
case ($num > 4.25):
$image="stars-4-5.gif";
break;
case ($num > 3.75):
$image="stars-4-0.gif";
break;
case ($num > 3.25):
$image="stars-3-5.gif";
break;
case ($num > 2.75):
$image="stars-3-0.gif";
break;
case ($num > 2.25):
$image="stars-2-5.gif";
break;
case ($num > 1.75):
$image="stars-2-0.gif";
break;
case ($num > 1.25):
$image="stars-1-5.gif";
break;
case ($num > .75):
$image="stars-1-0.gif";
break;
default:
$image="none";
}
return $image;
}
include("mysql_config.php");
$query = "SELECT AVG(rating) as avrating, COUNT(*) as count FROM Ratings";
$result = mysql_query($query,$link) or die(mysql_error());
$count = mysql_result($result, "0", "count");
$average = number_format(mysql_result($result, "0", "avrating"), 2);
$star = getStars($average);
if($star != "none"){
print"<span class="rating">Average reader rating:
<img src="stars/".$star."">(".$count." review";
if($count > 1)
print"s";
print")</span>";
}
?>