Results 1 to 4 of 4

 

Thread: Get Average Review Rating from MySQL

  1. #1
    accelerator's Avatar
    Online shopping rocks!

    Status
    Online
    Join Date
    Nov 2004
    Location
    England
    Posts
    3,035
    Thanks
    54
    Thanked 183 Times in 164 Posts


    Hi All

    I have a Reviews table where people can rate products out of 10. I need to calculate the average review rating for a particular product. I think I can do this using the following MySQL query:

    Code:
    $sql4 = "SELECT avg(ReviewRating) FROM Reviews WHERE ReviewIsApproved=1 AND ProductId=" . $ProductId . ";";
    is the query right?? If not, how should it be done?

    Many thanks

  2. #2
    scriptmonkey's Avatar
    Oranges & Lemons

    Status
    Offline
    Join Date
    Jan 2009
    Location
    Worthing
    Posts
    1,608
    Thanks
    112
    Thanked 253 Times in 192 Posts
    That looks like it should do exactly what you ask from it, I presume that ReviewIsApproved is a yes/no field?

    One thing, I would change the order of the query because, again presuming your ProductId field is indexed, it would be slightly quicker to search based on that first.
    The trouble with the rat race is that even if you win you're still a rat.
    Time passes. Listen. Time passes. Dylan Thomas
    Ebay Alerts to your inbox

  3. #3
    ian
    Registered User

    Status
    Offline
    Join Date
    Nov 2004
    Posts
    1,045
    Thanks
    30
    Thanked 106 Times in 80 Posts
    Quote Originally Posted by scriptmonkey View Post
    That looks like it should do exactly what you ask from it, I presume that ReviewIsApproved is a yes/no field?

    One thing, I would change the order of the query because, again presuming your ProductId field is indexed, it would be slightly quicker to search based on that first.
    Is MYSQL really dumb enough to process a query in the order you specify?

  4. #4
    accelerator's Avatar
    Online shopping rocks!

    Status
    Online
    Join Date
    Nov 2004
    Location
    England
    Posts
    3,035
    Thanks
    54
    Thanked 183 Times in 164 Posts
    OK, thanks guys, I have this working now, for those interested there's a code snippet below. The first bit of code sees whether there are approved reviews for a given ProductId, and then if there are, the second bit of the code calculates an average via the SELECT AVG(ReviewRating) MySQL statement.

    Rgds

    Code:
    //Find out if there are approved reviews for a product
    //Find out how many rows are in the table 
    $sql5 = "SELECT COUNT(*) FROM Reviews WHERE ProductId LIKE $ProductId AND ReviewIsApproved=1";
    $result5 = mysql_query($sql5, $connection) or trigger_error("SQL", E_USER_ERROR);
    $r5 = mysql_fetch_row($result5);
    $numrows5 = $r5[0];
    
    
    
    //If there are approved reviews, calculate the average review rating
    If ($numrows5 > 0) {
    
    //Calculate AverageReviewRating
    $sql4 = "SELECT AVG(ReviewRating) FROM Reviews WHERE ProductId LIKE $ProductId AND ReviewIsApproved LIKE 1";
    $result4 = mysql_query($sql4);
    
    
    
    //obtain
    while($row4 = mysql_fetch_array($result4)){
          //mysql_num_rows($result4) always equal to 1 when calculating an average
    	  //echo mysql_num_rows($result4);
    	  $AverageReviewRating = $row4['AVG(ReviewRating)'];
    	  //format to 2 decimal places
    	  $AverageReviewRating = number_format($AverageReviewRating, 2);
          echo " Average Rating: " . $AverageReviewRating . " / 10";
          }
    
    //End if there are approved reviews 
    }



Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Review and Rating Script
    By internet-laptop in forum Programming
    Replies: 1
    Last Post: 10-11-08, 11:47 AM
  2. Review / Rating Software
    By ian-d in forum Programming
    Replies: 11
    Last Post: 06-07-07, 09:29 AM
  3. Merchants Rating...
    By mike_01 in forum Affiliate Future
    Replies: 11
    Last Post: 20-02-06, 06:02 AM
  4. PHP-MYSQL working out average results
    By Wardy in forum Programming
    Replies: 8
    Last Post: 25-01-06, 01:23 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
To Top

Content Relevant URLs by vBSEO 3.5.0 RC2