Grouping using while loop
I assume the code you provide works and when it echo out the info the results look like this:
year quanitity name
2012 10 john
2012 20 mark
You want the output to look like this:
year : 2012
name : john quantity : 10
name : mark quantity : 20
Am I correct?
LastMitch
Industrious Poster
4,144 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45
There's a couple of ways you can do this:
$query = mysql_query("SELECT quanitity FROM table");
$sum = 0;
while($row = mysql_fetch_array($query)) {
$sum += $row['quanitity'];
}
echo $sum;
Add echo $sum; to your code above and it will echo the sum of the numbers
or you can try used SUM() function:
$query = "SELECT SUM(quanitity) FROM TABLE";
list($sum) = mysql_fetch_row($query);
<?php echo $sum; ?>
LastMitch
Industrious Poster
4,144 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45
$query = "SELECT SUM(quanitity) FROM `tablename` GROUP BY `year`";
Backquotes might also be important if you use names that are same as mysql keywords.
broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13
To get sum along with year use following query.
$query = "SELECT DISTINCT (
`year`), SUM(quanitity) FROM `tablename` GROUP BY `year`";
IIM
Practically a Master Poster
636 posts since Jun 2011
Reputation Points: 127
Solved Threads: 136
Skill Endorsements: 7