Rating average is unable to compute.

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
User avatar
Shinmeiryu
Forum Commoner
Posts: 29
Joined: Wed Nov 13, 2002 2:57 am

Rating average is unable to compute.

Post by Shinmeiryu »

I was working on a code addition to have ratings display selected from the discussions table, which works and displays on each individual discussion.

However the problem for gathering up the total counts of ratings submitted from each discussion, it will display only zero as the number, and doesn't seem to recognize computing the average of all the ratings.

The $outcome I thought should act like any mysql result. And the $access variable was intended to access within the array.

Am I missing something or misarranged something?

Code: Select all

<?php

@$outcome=mysql_query("SELECT rating FROM discussions WHERE articleID='$articleID'");
require("admin/failure.php");
while($access=mysql_fetch_array($outcome))
{

if (!$outcome) { 
   print "Error: no results returned (rating)"; 
} 

$rate_count = mysql_num_rows($outcome); 

if ($rate_count) { 
   for ($i=0; $i<$rate_count; $i++) { 
      $rating = mysql_fetch_array($outcome); 
      $total += $rating[rating]; 
   } 
   $average = $total / $rate_count; 
}

$average=$access["average"];
echo '<p><font face="Arial, Helvetica, sans-serif" size="2">',number_format($average,1,'.',''),'</font></p>';

}

?>
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Hello.
I have recieved your e-mail, I will do my best to help you.

First of all, this is my code:

Code: Select all

<?php  
if (empty($rating) || !is_numeric($rating)) 
   echo "Go back and select a rating";  
else  
{  
   $db=mysql_connect("localhost", "", "") or die(mysql_error()); 
   mysql_select_db("", $db) or die(mysql_error()); 

   $query = "SELECT number, total FROM evaluation WHERE id='$jokeId'"; //total is the total value of all votes.
   $result = mysql_query($query, $db) or die(mysql_error()); 
   $row = mysql_fetch_row($result) or die('no such joke'); 
   $row[0] += 1; 
   $row[1] += $rating; 
    
  $secondquery = 'UPDATE evaluation SET total='.$row[1].',number='.$row[0].' WHERE id='.$jokeId;
  $secondresult = mysql_query($secondquery, $db) or die(mysql_error()); 
  $average=$row[1]/$row[0];
  $thirdquery = 'UPDATE evaluation SET average='.$average.' WHERE id='.$jokeId;
  $thirdresult = mysql_query($thirdquery, $db) or die(mysql_error());
   echo "success, vote entered"; 
} 
?>
Your code is a little more complicated than mine because I do my calculations from the values that are in the database.

I hope this helps a little.

Cheers.
User avatar
nathus
Forum Commoner
Posts: 49
Joined: Thu Dec 12, 2002 6:23 pm

Post by nathus »

First of all, you need to call the mysql_num_rows before you do a fetch.

second of all, I'm not really sure why you have the loop

Code: Select all

while($access=mysql_fetch_array($outcome))
what you're doing is fetching a row of the results and storing it in $access, which removes it from the list. then calling the mysql_num_rows function to get the number, which will be one less than what it should be.

next you're calculating the average of the rest of the results in the database.

and then you're setting $average to equal $access["average"] which is where your zero is coming from. $acess is a result set from your query that is only selecting rating, so when you try to look at the array index "average" which doesn't exist, you get a zero, which is what you're printing out.

*edit* actually, after thinking about this a bit, it gives you an empty string "", which when passed to the number format is what gives you the 0.0

hopefully this explanation helps a little.

I would do it like this.

Code: Select all

<?
@$outcome=mysql_query("SELECT rating FROM discussions WHERE articleID='$articleID'"); 
require("admin/failure.php"); 

$rate_count = mysql_num_rows($outcome);

if ($rate_count) {

 for ($i=0; $i<$rate_count; $i++) {  
      $rating = mysql_fetch_array($outcome);  
      $total += $rating[rating];  
   }  
   $average = $total / $rate_count;  
   echo '<p><font face="Arial, Helvetica, sans-serif" size="2">',number_format($average,1,'.',''),'</font></p>'; 


}

?>

hopefully this works, and helps ya out.
User avatar
Shinmeiryu
Forum Commoner
Posts: 29
Joined: Wed Nov 13, 2002 2:57 am

Post by Shinmeiryu »

Thanks to the both of you guys for helping.

You were right about average not existing, and had to get rid of the access and other fetch in while.
Post Reply