How to create an archive list in php?

流过昼夜 提交于 2020-01-11 07:12:06

问题


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.


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/9296727/how-to-create-an-archive-list-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!