marklarah Posted April 6, 2008 Share Posted April 6, 2008 Sorry, I know this is a n00b question (im almost embarrassed to ask it) but im doing a rating system. So a have a table in my DB with each row representing a rate, with a few feilds. So lets say there are 3 rows (three people have rated). How would I work out the average rate and round it to no decimal places? What im asking is how do you add up all the values of a field in multiple rows? Quote Link to comment Share on other sites More sharing options...
unsider Posted April 6, 2008 Share Posted April 6, 2008 $total = ($value1 + $value2 + $value3) / $number.of.ratings(3 in this case) Use round() to round the final value ($total) echo "$total"; http://us.php.net/manual/en/function.round.php Quote Link to comment Share on other sites More sharing options...
marklarah Posted April 6, 2008 Author Share Posted April 6, 2008 I know how to add variables, I want to add, lets say my result returned 3 rows. How do I add up the data from the three rows. Lets say it was 6, 7 and 4 in the rows. How do I add them? Quote Link to comment Share on other sites More sharing options...
unsider Posted April 6, 2008 Share Posted April 6, 2008 You retrieve the values from those 3 rows and store each of them in a variable. Quote Link to comment Share on other sites More sharing options...
marklarah Posted April 6, 2008 Author Share Posted April 6, 2008 For christs sake its in a loop. But never mind, its sum. Quote Link to comment Share on other sites More sharing options...
AndyB Posted April 6, 2008 Share Posted April 6, 2008 http://www.tizag.com/mysqlTutorial/mysqlsum.php = SUM http://www.tizag.com/mysqlTutorial/mysqlavg.php = AVERAGE Either should help Quote Link to comment Share on other sites More sharing options...
marklarah Posted April 6, 2008 Author Share Posted April 6, 2008 Yeah its ok now it works. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.