Jump to content


Photo

Grouping using while loop

while loop

  • Please log in to reply
8 replies to this topic

#1 Syazri

Syazri

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 24 April 2013 - 04:12 AM

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

 

 

 



#2 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,268 posts
  • LocationCheshire, UK

Posted 24 April 2013 - 05:48 AM

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
}

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#3 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,268 posts
  • LocationCheshire, UK

Posted 24 April 2013 - 06:40 AM

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

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#4 Syazri

Syazri

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 24 April 2013 - 05:30 PM

Thanks @guru.. but is this php?



#5 Syazri

Syazri

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 24 April 2013 - 05:54 PM

Im sorry.. i dont know how to translate into php language



#6 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 24 April 2013 - 05:55 PM

https://www.google.c...ine pseudo code

Try.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#7 Syazri

Syazri

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 24 April 2013 - 06:07 PM

Thanks for the link.. is there any other example that will answer my question? its been 4 days im searching for the answer..



#8 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 24 April 2013 - 06:29 PM

I can write it for you, but it'll cost you. *shrug*
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#9 JaysonDotPH

JaysonDotPH

    Advanced Member

  • Members
  • PipPipPip
  • 178 posts
  • LocationPhilippines

Posted 24 April 2013 - 08:41 PM

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com