简体   繁体   English

按年份和月份的PHP MYSQL博客存档菜单

[英]PHP MYSQL Blog Archive Menu by Year and Month

I'm looking for an efficient way to collate all blog posts into a menu of the following format: 我正在寻找一种有效的方法来将所有博客文章整理成以下格式的菜单:

2012 2012

  • August(6) 八月(6)
  • September(4) 九月(4)
  • October(2) 十月(2)

Month representing the month(obviously), and the value inside the brackets representing the number of posts in that month. 表示月份的月份(显然),括号内的值表示该月份的帖子数量。 Once clicked, a search will then be made for all posts in that month, in that year. 点击后,将对该年当月的所有帖子进行搜索。

I need it to be dynamic, picking up November automatically when a post is created in that month, and carrying on into December, into 2013 etc etc... 我需要它是动态的,当一个帖子在那个月创建时自动拾取11月,并持续到12月,到2013年等等...

All I have is a UNIX timestamp for each post. 我所拥有的只是每个帖子的UNIX时间戳。 I would really like to avoid using seperate functions to gather endless comlex arrays etc. 我真的想避免使用单独的函数来收集无数的comlex数组等。

Any help much appreciated. 任何帮助非常感谢。

From your question, I understand you're trying to come up with a query to group a number of elements by month and year. 从您的问题中,我了解到您正在尝试提出一个查询,按月和按年对多个元素进行分组。 The following should do the trick: 以下应该做的伎俩:

SELECT 
    YEAR(dateField) AS YEAR, 
    MONTH(dateField) AS MONTH,
    COUNT(*) AS TOTAL 
FROM table 
GROUP BY YEAR, MONTH

Obviously, "dateField" being the name of your datetime/timestamp column and "table" being the name of your table. 显然,“dateField”是日期时间/时间戳列的名称,“table”是表的名称。

More information on the GROUP BY clause and aggregate functions (such as the COUNT(*) function used above) here . 有关GROUP BY子句和聚合函数(例如上面使用的COUNT(*)函数)的更多信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM