# How do I do a 5 star rating calculation?

Page 1 of 1

## 1 Replies - 6528 Views - Last Post: 08 April 2009 - 11:35 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=98071&amp;s=fd01bcc8105bfc406a6a56c2222a851f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 EvolutionMedia

Reputation: 3
• 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?

• Saucy!

Reputation: 6246
• Posts: 24,014
• 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;
echo ("Enter number of stars: ");
\$rating = rtrim(fgets(STDIN), "\n");
\$totalRating = 0;
while (\$rating > 0 && \$rating <= \$MaxRating)
{
\$totalRating += \$rating;
echo ("Enter number of stars: ");
\$rating = rtrim(fgets(STDIN), "\n");
}