Jump to content

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

if there is a selection option for year to show up the following you want.

$query_list("SELECT year, quantity, name from BDNAME WHERE year = '$selected_year'");
$query_result = mysql_query($query_list);


thin apply from guru suggestion and others.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.