if($stmt = $connection->prepare("SELECT rating,nov FROM content_rating WHERE content_rating.cont_id=?")){
$stmt->bind_param('i',$cont_id);
$stmt->execute();
$result = $stmt->get_result();
$row=$result->fetch_object();
}else{
echo $connection->error;
}
if($result->num_rows>0){ // record exist for the page
$score_avg=number_format(($row->rating/$row->nov),2,".","");
//($score/$nov);
echo "Average ratting:$score_avg : "; // display rating value
$rt=round($score_avg);
// display the rating by start images.
$img="";
$i=1;
while($i<=$rt){
$img=$img."<img src=images/star.gif >";
$i=$i+1;
}
while($i<=5){
$img=$img."<img src=images/star2.gif >";
$i=$i+1;
}
echo $img;
The above code can be modified to get formatted average value from MySQL table itself and there is no need to format in php side. You can read the details on MySQL data format here. The modified code is given here. Note that you can use any one of them for your displaying the ratings.
"SELECT format((rating/nov),0) as score_avg FROM content_rating WHERE content_rating.cont_id= $cont_id"
Full code is here. disp.php
<?Php
if($stmt = $connection->prepare("SELECT rating,nov FROM content_rating WHERE content_rating.cont_id=?")){
$stmt->bind_param('i',$cont_id);
$stmt->execute();
$result = $stmt->get_result();
$row=$result->fetch_object();
}else{
echo $connection->error;
}
if($result->num_rows>0){ // record exist for the page
$score_avg=number_format(($row->rating/$row->nov),2,".","");
//($score/$nov);
echo "Average ratting:$score_avg : "; // display rating value
$rt=round($score_avg);
// display the rating by star images.
$img="";
$i=1;
while($i<=$rt){
$img=$img."<img src=images/star.gif >";
$i=$i+1;
}
while($i<=5){
$img=$img."<img src=images/star2.gif >";
$i=$i+1;
}
echo $img;
}
?>
That is the end of our rating script. We can add more features like flood interval, IP checking etc to the script.
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.