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? Link to comment https://forums.phpfreaks.com/topic/99838-n00b-question/ 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 Link to comment https://forums.phpfreaks.com/topic/99838-n00b-question/#findComment-510606 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? Link to comment https://forums.phpfreaks.com/topic/99838-n00b-question/#findComment-510613 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. Link to comment https://forums.phpfreaks.com/topic/99838-n00b-question/#findComment-510618 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. Link to comment https://forums.phpfreaks.com/topic/99838-n00b-question/#findComment-510621 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 Link to comment https://forums.phpfreaks.com/topic/99838-n00b-question/#findComment-510623 Share on other sites More sharing options...
marklarah Posted April 6, 2008 Author Share Posted April 6, 2008 Yeah its ok now it works. Link to comment https://forums.phpfreaks.com/topic/99838-n00b-question/#findComment-510758 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.