If you use a while loop for all results, you can store the previous year, and check whether it has changed in the next loop. If it has changed, output the year, otherwise don't.
$year = 0;
while ($result = ...) {
if ($year <> $result['year']) {
$year = $result['year'];
echo $year . '<br/>';
}
echo $result['month']; // etc.
}
pritaeas
Posting Prodigy
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86
"SELECT ... YEAR(`date`) AS `year`, MONTH(`date`) AS `counter`, MONTHNAME(`date`) AS `month`, SUM(`prices`) AS total GROUP BY `year`, `counter` ORDER BY `year`,`counter`"
Ensure you then echo year, month and total in a loop to produce the table.
Is that it?
//EDIT
Just saw that there was a duplicate thread. :(
diafol
Keep Smiling
10,613 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57
Just saw that there was a duplicate thread. :(
Don't think it is, the other one was about the query, this one is about displaying the result.
pritaeas
Posting Prodigy
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86
So it now seems. I read:
So do I need to do a query for each year and month grouping - step and repeat?
And got the wrong end of the stick?
OK, thanks Prit.
diafol
Keep Smiling
10,613 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57
Show what you have now, and we'll help you fix it.
pritaeas
Posting Prodigy
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86
Something like this:
$year = 0;
while ($row = $result->fetch_object()) {
if ($year <> $row->reg_year) {
echo '<br/>Year ' . $row->reg_year . '<br/>';
$year = $row->reg_year;
}
printf ("%s (%s)<br/>", $row->reg_month, $row->reg_count);
}
pritaeas
Posting Prodigy
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86
1. $year stores 0
3. compares if the year in the current row is not equal (<>) to $year
4/5. if it is not, it prints the year, and copies it to $year for the next row to check
pritaeas
Posting Prodigy
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86
Question Answered as of 1 Year Ago by
pritaeas,
diafol
and
jstfsklh211