We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,842 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Grouping using while loop

Example of my record :

year  quanitity name
2012     10     john
2012     20     mark
2013     30     david
2013     40     alex
2014     50     stacy



while (!$report->EOF){
        if(is_null($year) || $year <> $report->fields['year']) {
        $year = $report->fields['year'];
        ?>
        <tr><td align="center" colspan="2" >Year : </td><td><?=$year ?></td></tr>
        <?
            }
        ?>    
        <tr><td align="center" colspan="2" >Quantity : </td><td><?=$report->fields['quanitity'] ?></td></tr>
        <?
        $report->MoveNext();                          
        }

How to do so the result goes like this

year : 2012
name : john quantity : 10
name : mark quantity : 20

Total : 30

year : 2013
name : david quantity : 30
name : alex quantity : 40
Total : 70

Thanks in advance

4
Contributors
7
Replies
9 Hours
Discussion Span
2 Months Ago
Last Updated
43
Views
wuzere.crewzz
Newbie Poster
15 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

the output look like this

year : 2012
name : john quantity : 10
name : mark quantity : 20

i just want add 'Total' for each year.

wuzere.crewzz
Newbie Poster
15 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

Thanks for your answer.. but your answer will sum all year..
I just want total for each year..

wuzere.crewzz
Newbie Poster
15 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

i still didnt get the correct answer

wuzere.crewzz
Newbie Poster
15 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
$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

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1553 seconds using 2.7MB