Syazri Posted April 24, 2013 Share Posted April 24, 2013 This is 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 1) group by years 2) total quantity for each year year : 2012name : john quantity : 10name : mark quantity : 20 Total : 30 year : 2013name : david quantity : 30name : alex quantity : 40Total : 70 Quote Link to comment https://forums.phpfreaks.com/topic/277244-grouping-using-while-loop/ Share on other sites More sharing options...
Barand Posted April 24, 2013 Share Posted April 24, 2013 pseudocode prevYear = 0 total = 0 while fetch next row { if year <> prevYear { if prevyear <> 0 { output total } output year prevYear = year } output row data total += quantity } Quote Link to comment https://forums.phpfreaks.com/topic/277244-grouping-using-while-loop/#findComment-1426279 Share on other sites More sharing options...
Barand Posted April 24, 2013 Share Posted April 24, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/277244-grouping-using-while-loop/#findComment-1426288 Share on other sites More sharing options...
Syazri Posted April 24, 2013 Author Share Posted April 24, 2013 Thanks @guru.. but is this php? Quote Link to comment https://forums.phpfreaks.com/topic/277244-grouping-using-while-loop/#findComment-1426448 Share on other sites More sharing options...
Syazri Posted April 24, 2013 Author Share Posted April 24, 2013 Im sorry.. i dont know how to translate into php language Quote Link to comment https://forums.phpfreaks.com/topic/277244-grouping-using-while-loop/#findComment-1426452 Share on other sites More sharing options...
Jessica Posted April 24, 2013 Share Posted April 24, 2013 https://www.google.com/search?q=define+pseudo+code Try. Quote Link to comment https://forums.phpfreaks.com/topic/277244-grouping-using-while-loop/#findComment-1426453 Share on other sites More sharing options...
Syazri Posted April 24, 2013 Author Share Posted April 24, 2013 Thanks for the link.. is there any other example that will answer my question? its been 4 days im searching for the answer.. Quote Link to comment https://forums.phpfreaks.com/topic/277244-grouping-using-while-loop/#findComment-1426458 Share on other sites More sharing options...
Jessica Posted April 24, 2013 Share Posted April 24, 2013 I can write it for you, but it'll cost you. *shrug* Quote Link to comment https://forums.phpfreaks.com/topic/277244-grouping-using-while-loop/#findComment-1426459 Share on other sites More sharing options...
Yohanne Posted April 25, 2013 Share Posted April 25, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277244-grouping-using-while-loop/#findComment-1426467 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.