5 Star Rating problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
p3rk5
Forum Commoner
Posts: 34
Joined: Thu Jan 29, 2009 10:19 pm

5 Star Rating problems

Post by p3rk5 »

I'm using this code to show an amount of stars depending on what the overall rating is:

Code: Select all

$query = "SELECT * FROM `rating` WHERE `id`='$db_id'";
            $result = mysql_query($query);
            $totalrows = mysql_num_rows($result);
            if ($totalrows == 0) {
                $stars = '<span>Not Rated</span>';
            } else {
                $rate = mysql_fetch_array($result);
                $rating_value = array_sum($rate['rating_value']);
                $rating = round($rating_value/$totalrows,1);
                if ($rating <= 1.4) {
                    $stars = '<img src="images/star.gif">';
                } elseif ($rating <= 2.4) {
                    $stars = '<img src="images/star.gif"><img src="images/star.gif">';
                } elseif ($rating <= 3.4) {
                    $stars = '<img src="images/star.gif"><img src="images/star.gif"><img src="images/star.gif">';
                } elseif ($rating <= 4.4) {
                    $stars = '<img src="images/star.gif"><img src="images/star.gif"><img src="images/star.gif"><img src="images/star.gif">';
                } else {
                    $stars = '<img src="images/star.gif"><img src="images/star.gif"><img src="images/star.gif"><img src="images/star.gif"><img src="images/star.gif"><img src="images/star.gif"><img src="images/star.gif"><img src="images/star.gif"><img src="images/star.gif"><img src="images/star.gif">';
                }
            }
However when I try to view the page I get this error: "Warning: array_sum() [function.array-sum]: The argument should be an array in /var/www/music/view.php". Any idea why It's doing this?
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: 5 Star Rating problems

Post by susrisha »

Code: Select all

 
 $rate = mysql_fetch_array($result);
                $rating_value = array_sum($rate['rating_value']);
 
here $rate['rate_value'] just gives one value of the result and not the full array.
Post Reply