How do I do a 5 star rating calculation?

It turns up not the number I want.

Page 1 of 1

1 Replies - 6068 Views - Last Post: 08 April 2009 - 11:35 AM Rate Topic: -----

#1 EvolutionMedia  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 125
  • Joined: 11-August 08

How do I do a 5 star rating calculation?

Posted 08 April 2009 - 10:25 AM

So, I'm working on a five star calculation. It writes to the database after the user votes then in the script it takes the total votes and divides it by rating.

So, the rating increases as each star is giving. So for example.

10 Rating out of 2 votes = five stars to give out.

or

15 Rating out of 3 stars = Five stars to give out.

See what I mean?

The Code looks likes this:


$ProductRating = $TotalVotes / $Rating;



Maybe there's something wrong with the if. Here's the if conditions.


if($ProductRating == 0 ) {
$RatingOutput = '<span><img src="../imgs/stars/star0.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star0.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star0.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star0.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star0.png" width="16" height="16"></span>';
}
if($ProductRating > 0 && $ProductRating < 2) {
$RatingOutput = '<span><img src="../imgs/stars/star1.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star0.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star0.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star0.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star0.png" width="16" height="16"></span>';
}
if($ProductRating > 1 && $ProductRating == 2) {
$RatingOutput = '<span><img src="../imgs/stars/star1.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star1.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star0.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star0.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star0.png" width="16" height="16"></span>';
}
if($ProductRating > 2 && $ProductRating <= 3) {
$RatingOutput = '<span><img src="../imgs/stars/star1.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star1.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star1.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star0.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star0.png" width="16" height="16"></span>';

}
if($ProductRating == 4 && $ProductRating > 3 ) {
$RatingOutput = '<span><img src="../imgs/stars/star1.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star1.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star1.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star1.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star0.png" width="16" height="16"></span>';

}
if($ProductRating == 5 || $ProductRating > 5) {
$RatingOutput = '<span><img src="../imgs/stars/star1.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star1.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star1.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star1.png" width="16" height="16"></span>';
$RatingOutput .= '<span><img src="../imgs/stars/star1.png" width="16" height="16"></span>';
}




I voted five stars for this one download I have. I only have one vote. It's coming up with one star. So, it's possible it's with the If function. What do you guys think?

Is This A Good Question/Topic? 0
  • +

Replies To: How do I do a 5 star rating calculation?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: How do I do a 5 star rating calculation?

Posted 08 April 2009 - 11:35 AM

Don't you want
$ProductRating = $Rating / $TotalVotes;
?

Otherwise for your stated entry, $ProductRating = 1 / 5, or 0.2.

Try this little sample command line program for size:
<?php
function GetStars($rating, $maxRating)
{
   $ratingOutput = "";
   for ($i = 0; $i < $maxRating; ++$i)
   {
      $ratingOutput .= '<span><img src="../imgs/stars/star' . ($i < $rating ? '\
1' : '0') . '.png" width="16" height="16" /></span>' . "\n";
   }
   return $ratingOutput;
}

$MaxRating = 5;
$rating = 0;
$numVotes = 0;
echo ("Enter number of stars: ");
$rating = rtrim(fgets(STDIN), "\n");
$totalRating = 0;
while ($rating > 0 && $rating <= $MaxRating)
{
   $numVotes++;
   $totalRating += $rating;
   echo ("Enter number of stars: ");
   $rating = rtrim(fgets(STDIN), "\n");
}

echo ("Output for $totalRating with $numVotes (" . $totalRating/$numVotes . "):\
 \n");
echo GetStars($totalRating / $numVotes, $MaxRating);

?>

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1