首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用current_timestamp用PHP和MySQL生成站点地图

用current_timestamp用PHP和MySQL生成站点地图
EN

Stack Overflow用户
提问于 2013-03-21 22:12:35
回答 1查看 112关注 0票数 0

我有MySQL数据库记录: post_id int、post_title text、post_date timestamp (默认值为current_timestamp)

如何使用PHP生成如下结构的站点地图,结果中没有空的日期节点(其中的帖子仅来自数据库),例如:

代码语言:javascript
运行
复制
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

等。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-21 22:35:30

首先,你必须选择你的数据,以便在你的结构中打印出来。应该是这样的:

代码语言:javascript
运行
复制
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; 

然后遍历结果并构建一个以年、月和日为索引的多维索引。其中一个条目可能如下所示:

代码语言:javascript
运行
复制
$data[2013][10][31] = array("post1", "post2");

有了这些,你就可以很容易地打印出你的网站地图(只需要几个foreach结构)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15549933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档