Jump to content

Grouping using while loop


Syazri

Recommended Posts

This is example of my record:

 

 

  1. year quanitity name
  2. 2012 10 john
  3. 2012 20 mark
  4. 2013 30 david
  5. 2013 40 alex
  6. 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

1) group by years

2) total quantity for each year

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

Total : 30

 

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

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/277244-grouping-using-while-loop/
Share on other sites

correction to above pseudocode

prevYear = 0
total = 0
while fetch next row {
    if year <> prevYear {
        if prevyear <> 0 {
            output total
            total = 0             // added
        }
        output year
        prevYear = year
    }
    output row data
    total += quantity
}
output total              // added

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.