我有MySQL数据库记录: post_id int、post_title 文本、post_date 时间戳(默认值为 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
等等
最佳答案
首先,您必须选择数据,以便可以方便地将其打印在您的结构中。应该是这样的:
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 结构)。
关于php - 使用 PHP 和 MySQL 通过 current_timestamp 生成站点地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15549933/