Page 1 of 1

5 Star Rating problems

Posted: Tue Feb 17, 2009 10:01 pm
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?

Re: 5 Star Rating problems

Posted: Tue Feb 17, 2009 10:16 pm
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.