SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Non-Member
    Join Date
    Nov 2003
    Location
    here
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    add fields in a query

    im making a rating system which stores each rating in a table, whats the best way to add all the rating fields together so i have a total rating value to work with to work out an average rating etc.

  2. #2
    SitePoint Evangelist -Oz-'s Avatar
    Join Date
    Nov 2001
    Location
    Phoenix, AZ, USA
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql has a sum commend:
    Code:
    SELECT SUM(value) FROM table
    for average you could even do:
    Code:
    SELECT (SUM(value)/COUNT(value)) FROM table
    Oz
    GamersMark - On Target Gaming
    OzTheory - Programming and Web Solutions
    AmIBlocked - Check if you've been blocked on IM

  3. #3
    Non-Member
    Join Date
    Nov 2003
    Location
    here
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    appreciated (Y)

  4. #4
    Non-Member
    Join Date
    Nov 2003
    Location
    here
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It worked, made things very easy thanks:

    PHP Code:
    function getMemberRating($username) {

    global 
    $database// Access global objects

    // Get all ratings for this user
    $q "SELECT (SUM(rating)/COUNT(rating)) AS ratingaverage FROM ratings WHERE rating_receiver = '$username'";
    $result mysql_query($q);
    $array mysql_fetch_array($result);
    return 
    round($array["ratingaverage"]);

    // End getMemberRating() 

  5. #5
    SitePoint Evangelist -Oz-'s Avatar
    Join Date
    Nov 2001
    Location
    Phoenix, AZ, USA
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    glad to hear it worked perfectly. and nice of you to post the code so others can see.
    Oz
    GamersMark - On Target Gaming
    OzTheory - Programming and Web Solutions
    AmIBlocked - Check if you've been blocked on IM


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
  •