Results 1 to 9 of 9

 

Thread: PHP-MYSQL working out average results

  1. #1
    This is the one

    Status
    Offline
    Join Date
    Aug 2003
    Posts
    2,882
    Thanks
    29
    Thanked 9 Times in 7 Posts


    I guess this shouldn't be too hard but would appreciate any help.

    Ia hev a database that contains a rating (ranging from 1-10) that users click on to review something.
    Insteda of now displaying all the individual results from the votes I want to just show the average.
    Anyone able to shopw me the PHP needed to do this please...


    Cheers
    Wardy

  2. #2
    Registered User

    Status
    Offline
    Join Date
    Aug 2003
    Location
    Great Yarmouth, UK
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i do this in a few places, and prob go a long way about it, but this is what i do.

    I have a field called total, each time there's a vote i add the vote value to this field. So if the first 2 people vote 10, then this field holds 20.

    I have a field called votes. With each vote for that particular product i increment the value by 1.

    So at this stage i know for product a, i've got 10 votes with a total of 50.

    Simple maths gives you the rest. ie total/votes = average.

  3. #3
    Registered User

    Status
    Offline
    Join Date
    Mar 2004
    Location
    Stafford, UK
    Posts
    323
    Thanks
    1
    Thanked 4 Times in 2 Posts
    You can use SQL's built in math functions to do this.
    Code:
    SELECT AVG(rating) AS average FROM sometable;
    PHP would look something like this (database stuff included for completeness, but you've probably got all that already):

    PHP Code:
    $link mysql_connect("localhost","username","password");
    mysql_select_db("somedatabase",$link);
    $sql "SELECT AVG(rating) AS average FROM sometable";
    $result mysql_query($sql,$link);
    $row mysql_fetch_array($result);
    $average $row["average"]; 
    Hope this helps!
    Developer of the Price Tapestry Price Comparison Script now with full WordPress Plugin!

  4. #4
    This is the one

    Status
    Offline
    Join Date
    Aug 2003
    Posts
    2,882
    Thanks
    29
    Thanked 9 Times in 7 Posts
    Thanks for the replies guys, the AVG function was exacly what I was after

    1 small snag is that I want to round the results to 1 decimal place, I know it requires the "round" function but having trouble gettign ti working correctly. When I add the round function it's just showing the nearest whole number and not to the 1 decimal place.

    $result = mysql_query("SELECT AVG(score) FROM game1");
    echo round ($result,1);

    Anyone any suggestions what glaring mistake I'm making now?!

    Cheers
    Wardy

  5. #5
    Registered User

    Status
    Offline
    Join Date
    Mar 2005
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The following should work:
    mysql_query("SELECT Round(AVG(score),1) FROM game1");

  6. #6
    Registered User

    Status
    Offline
    Join Date
    Mar 2004
    Location
    Stafford, UK
    Posts
    323
    Thanks
    1
    Thanked 4 Times in 2 Posts
    Hiya,

    Your code seems to be missing the line where you extract the actual average value from the query result. Exactly what you have posted would just round the value of the database resource handle....

    Try this:

    PHP Code:
    $sql "SELECT AVG(rating) AS average FROM sometable";
    $result mysql_query($sql,$link);
    $row mysql_fetch_array($result);
    $average round($row["average"],1); 
    Developer of the Price Tapestry Price Comparison Script now with full WordPress Plugin!

  7. #7
    Registered User

    Status
    Offline
    Join Date
    Aug 2003
    Posts
    2,448
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You need to get the result before rounding it. i.e

    $result = mysql_query("SELECT AVG(score) FROM game1");
    $row=mysql_fetch_row($result);
    echo round ($row[0],1);

    but I get the feeling that might just be a typo.

    The result is 4 then round will still return 4, it won't make it 4.0. number_format will probably do better for you.
    number_format(4,1)="4.0"
    number_format(3.55,1)="3.6"

    If you are still getting whole numbers, check what the result is from mysql before doing any processing.

  8. #8
    Registered User

    Status
    Offline
    Join Date
    Mar 2005
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry for not being clear, I was refering to the MySQL ROUND function as opposed to the php round function. Or is there something wrong with the MySQL ROUND function?

  9. #9
    This is the one

    Status
    Offline
    Join Date
    Aug 2003
    Posts
    2,882
    Thanks
    29
    Thanked 9 Times in 7 Posts
    Cool, nice one guys. Working a treat now thanks

    Cheers
    Wardy



Thread Information

Users Browsing this Thread

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

Similar Threads

  1. php scripts to display data from MySql
    By accelerator in forum Programming
    Replies: 3
    Last Post: 03-01-06, 02:26 AM
  2. How to make local php file working on remote server?
    By ukyellowpage in forum Programming
    Replies: 10
    Last Post: 21-02-05, 03:24 PM
  3. Anyone anygood with xml, php, mysql and Affiliate Window
    By AnnonnyMouse in forum Affiliate Marketing Lounge
    Replies: 1
    Last Post: 23-09-04, 11:00 AM
  4. php and mysql search
    By andy_jacko in forum Programming
    Replies: 6
    Last Post: 13-09-04, 12:12 PM
  5. PHP & MYSQL
    By Affiliates4u in forum Programming
    Replies: 8
    Last Post: 13-01-03, 08:12 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