我有MySQL数据库记录: post_id int、post_title text、post_date timestamp (默认值为current_timestamp)
如何使用PHP生成如下结构的站点地图,结果中没有空的日期节点(其中的帖子仅来自数据库),例如:
2010
- 2010 Jan
-- 2010 Jan 10
--- post-1
--- post-2
-- 2010 Jan 12
--- post-3
-- 2010 Jan 17
--- post-4
- 2010 Feb
-- 2010 Frb 05
--- post-5
2011
- 2011 Dec
-- 2011 Dec 02
--- post-6
--- post-7
-- 2011 Dec 21
--- post-8
等。
发布于 2013-03-21 22:35:30
首先,你必须选择你的数据,以便在你的结构中打印出来。应该是这样的:
SELECT day(post_date) as day, month(post_date) as month, year(post_date) as year,
DATE_FORMAT(post_date,"%Y-%m-%d") as created_at, post_title,
post_id FROM posts ORDER BY created_at;
然后遍历结果并构建一个以年、月和日为索引的多维索引。其中一个条目可能如下所示:
$data[2013][10][31] = array("post1", "post2");
有了这些,你就可以很容易地打印出你的网站地图(只需要几个foreach结构)。
https://stackoverflow.com/questions/15549933
复制相似问题