Jump to content

PHP / MySql Query and Group By Date


hoopplaya4

Recommended Posts

Hi All,

 

I'm currently querying some database fields that are dates.  They are all in the following format: 11/12/2008.

 

My question is this: How can I query the database to get all these fields, and then group them into separate (links) by Month.  For example, all the 11/xx/xxxx will be displayed under a link that would say "November."  Then users would click "November" and then all 11/xx/xxxx database entries would be displayed.

 

Is this going to be too complicated?  Or is there an easy way to achieve this?  Please let me know if I'm being vague!  Thanks.

Link to comment
https://forums.phpfreaks.com/topic/131976-php-mysql-query-and-group-by-date/
Share on other sites

That worked, thanks.  But the main point of my page is because it acts as an "archive" for all of the MySql records.  Thus, how might I display it in a way like below:

 

> 2008

 

> 2007

 

> 2006, etc

 

Then, a user will click the year, which will then spill out all the MySql records for that year.  Does that make sense?  Is there a tutorial anywhere out there that teaches how to make an archive?

well, i can think of 3 ways:

1) if you know the range of years, you can pass the year with the link as an argument like:

<a href="somePage.php?year=2008>2008</a>

and then $_GET['year'] on somePage.php, and query the DB table for all rows that match:

"SELECT * FROM tableName WHERE date LIKE '%{$_GET['year']}'";

using the example of 2008, this will find all rows which have the digits 2008 at the end of the value in the date column. then just display them on the page.

 

2) if you don't know the range of years, things get a little trickier, since you have to get dates from the DB, but the dates are treated as strings, so you have to get all the unique rows, and either get the year off each row (by using explode() or split() by the "/" character), and then making a list of the unique years, and then doing option 1 stated above.

or you could get all unique dates from the database, convert them to timestamps with strtotime(), and then find the highest timestamp value and the lowest timestamp value, getting the year value from each (by using date("Y", $timestamp)) and then looping through the whole range to populate the link list with all intermediate years and then doing option 1 stated above.

 

3) (again, if you don't know the range of years) change the table to use a DATE datatype for dates, and then just do:

"SELECT date FROM tableName ORDER BY date DESC LIMIT 1";

and get the year off of that. that will be your $yearMax.

then do:

"SELECT date FROM tableName ORDER BY date LIMIT 1";

this will be your $yearMin.

then loop through the intermediate years to populate the link list, and do option 1 as stated above.

 

there are possibly better ways to do this, but i'll let the big guyz come up with them.

Archived

This topic is now archived and is closed to further replies.

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