SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Not Bad, eh? Justin Sampson's Avatar
    Join Date
    Aug 2000
    Location
    N.S., Canada
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm trying to make a rating script for my site. I'm making it like what site point has so that you can select a number between 1 and 10. I was thinking that what ever number they add would be a row in the table then I could add them all up and divide them by the number of rows.

    I know how to do the division part but how could I add all the rows together?

    Thanks,
    Justin Sampson

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming you have a table named "Ratings" which contains a columns named "points" and "ID_of_thing_being_rated" you could find the average of all rows with the following SQL (assuming your PHP variable $id holds the value you are searching for).
    Code:
    SELECT ( SUM(points) / COUNT(points) ) AS average
    FROM Ratings
    WHERE ID_of_thing_being_rated = '$id'

  3. #3
    Not Bad, eh? Justin Sampson's Avatar
    Join Date
    Aug 2000
    Location
    N.S., Canada
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, thats exactly what I wanted. You just made my day

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I don't know what I was thinking when I wrote my post above - think I was tired.

    Of course sql know what an average is. so you can write the query:
    Code:
    SELECT AVG(points) as average
    FROM Ratings
    WHERE ID_of_thing_being_rated = '$id'
    Silly me!


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
  •