We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

PHP MySQLi Query for printing out year/dates in table format

I need helping thinking though something. I am pulling dates, Year, Month of that year and an amount. I then want to go to the next year. So, 2010 then 12 rows of the months with amounts, then 2011 then 12 rows of the months printed out with amounts...

Ex:
2010
January
February
March ...
...
2011
January
February ...

Does that make sense? So do I need to do a query for each year and month grouping - step and repeat?

or is Array the best way? (Not sure where to start here)

Any thoughts towards how to figure out would be great.
Thanks!

4
Contributors
13
Replies
2 Weeks
Discussion Span
1 Year Ago
Last Updated
14
Views
Question
Answered
timwhelan
Newbie Poster
18 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

one query ordered by year/month

year = '';
foreach row
    if row[year] != year
        print year
    end if
    do stuff
end loop
jstfsklh211
Junior Poster
100 posts since Apr 2011
Reputation Points: 34
Solved Threads: 27
Skill Endorsements: 1
"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
Moderator
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
Moderator
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
Moderator
10,613 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57

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.
}

Okay I am sure I nee to learn a little more. I tried this and no go. Plus this has been on hold for a week..

$year = 0;
while ($result = mysql_fetch_array($year)) {
  if ($year <> $result['year']) {
    $year = $result['year'];
    echo $year . '<br/>';
  }
  echo $result['month']; // etc.
}

I am also coding for mysqli so not sure if there is a different function
Thanks

timwhelan
Newbie Poster
18 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Show what you have now, and we'll help you fix it.

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

Okay sorry for the delay. Thank you for you assistance!

Here is the initial query you helped me with already.

if ($result = $db->query("SELECT YEAR(reg_date) AS 'reg_year', MONTHNAME(reg_date) AS 'reg_month', COUNT(*) AS 'reg_count' FROM micro_applicants GROUP BY YEAR(reg_date), MONTH(reg_date)")) {

What I am trying to accomplish is listing the Year, Months with count of sign ups, like so:
2012
January = 10
February = 3
2011
January = 7
February =11
March = 9
etc

Here is the code I started writing trying to figure this out.

for ( $years = $row->reg_year; $years <= $row->reg_year; ++$years) {
	echo "$years ";
	
	while ($row = $result->fetch_object(YEAR)) {
		foreach ($months AS $month) {
			if $months = $month {
			     echo "$month";
                        }
                 }
        }
}
timwhelan
Newbie Poster
18 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Okay so I have gotten this far...

/* HERE IS THE FIRST TEST LISTING EVERYTHING IN EACH LINE.*/
while ($row = $result->fetch_object()) {
echo '<br /> Year '. $row->reg_year .   "<br />";

	/* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {
        printf ("%s (%s)\n", $row["reg_month"], $row["reg_count"]);
    }

}

Which prints this:

Year 2010
November (7) December (6) January (4) February (6) March (2) April (2) May (5) June (3) July (16) August (10) September (7) October (7) November (3) December (9) January (4) February (3)

Now I need to figure out splitting it between the years correctly

timwhelan
Newbie Poster
18 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

That is awesome! Can you tell me what the <> means in line 3? Does that mean count through?
Does starting the $year =0; mean start counting and end when done?

So it goes through the years and print the months under that. Thanks. I will mark as solved after this.

Thank you for you assistance.

timwhelan
Newbie Poster
18 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

Thank you for all your help, I appreciate your patience. And thanks for the explanation. Trying to learn in every step.

Good Times!

timwhelan
Newbie Poster
18 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by pritaeas, diafol and jstfsklh211

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1098 seconds using 2.75MB