如何在 php 中创建存档列表?

发布于 2025-01-05 09:49:32 字数 285 浏览 5 评论 0原文

我正在创建我的公司博客,想知道如何创建一个存档页面,读者可以在其中单击月份/年份并显示该时间段的所有博客文章。

这些天我经常在博客上看到这个,并且想知道我自己如何创建它。

它看起来像这样:

  • 2012 年 7 月
  • 2012 年 6 月
  • 2012 年 3 月

显然,我希望通过引用我的博客表中的时间字段来动态创建列表,但从哪里开始呢?

有没有关于如何实现这一点的文档?

我正在从头开始创建自己的博客。

I am creating my companies blog and would like to know how to go about creating an archive page where the reader can click on the month/year and display all blog posts for that time period.

I see this very often on blogs these days and would like to know how I can myself create it.

It will look something like this:

  • July 2012
  • June 2012
  • March 2012

Obviously I would want the list created dynamically by referencing the time field in my blog table, but where to start?

Is there any documentation on how to implement this?

I am creating my own blog from scratch.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攀登最高峰 2025-01-12 09:49:32

如果您使用数据库,则可以简单地使用时间戳根据日期对文章进行分组。

如果您不使用数据库,则可以使用文件系统,并将每篇文章放在相关的结构中:

/articles/2011/July/article_name_here.html

If you are using a database, you can simply use timestamps to group articles based on date.

If you aren't using a database, you can use the filesystem, and place each article in a relevant structure:

/articles/2011/July/article_name_here.html

逆蝶 2025-01-12 09:49:32

鉴于您提到没有特定的框架/CMS,我将介绍我会采取的总体想法:

获取渲染的月份列表 渲染

SELECT Month(`date`), Year(`date`) FROM articles GROUP BY Month(`date`), Year(`date`)

给定时间跨度的文章列表

SELECT * FROM articles WHERE Date(`date`) = foo, Year(`date`) = bar

其余的只是渲染本身,基于您当前的实现太多,但是这应该给你一个开始的方法。

Given you mentioned no specific framework / CMS, I will present the general idea I would take:

Get the list of months to render

SELECT Month(`date`), Year(`date`) FROM articles GROUP BY Month(`date`), Year(`date`)

Render list of articles for given time span

SELECT * FROM articles WHERE Date(`date`) = foo, Year(`date`) = bar

The rest is just render itself, too much based on your current implementation, but this ought to give you a way to start from.

任性一次 2025-01-12 09:49:32

有一些方法可以解决这个问题。我能想到的最简单的方法是为每个月创建一个链接,将您带到一个包含该月所有帖子的 html 页面。(它需要每月手动执行) 。

另一种方法是使用 MySQL 来实现,并使所有过程动态且自动。
每次提交您的帖子时,每个月的参考链接都会保存到数据库中。

然后,您可以从数据库中回显月份,并为它们提供一个 php 文件的链接或您的语言,以便仅回显该月的帖子。您还需要传递变量。
你的链接会是这样的,

index.php?month=september2012

我的网站上有这个系统。
还有一些方法..比如CMS风格将您的内容保存到文本文件..但我还没有尝试过,因此我不能支持你。

我希望至少能让您了解如何做到这一点。

There are some ways to approach this.The most easy way i can think of is that you create a link for each month that takes you to an html page with all the post of that month.(It requires to do it manually every month).

Another way is to do it with MySQL and make all the process dynamic and automatic.
Each time your post is submited a reference link will be saved to the database for each months.

Then you can echo out the months from your database and give them a link to a php file or what your language is to echo out the post only from that month.You will be needed also to pass variables.
You link would be something like this

index.php?month=september2012

I have this system in site.
There also some ways .. like CMS style saving your to a text file.. but I haven't tried thus I cannot support you.

I hope a gave you at least some idea of how this could be done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文