Jump to content

sort database entries by month


fonster_mox

Recommended Posts

Hi, I do not know any php, just wanted to clear that up first!

 

I have a mysql database with the following fields: datetime, title, body and id. Where datetime is automatically inputted in the format

$datetime = date ("Y-m-d H:i:s");

 

What I would like now is a .php code which lists all months (eg, May 2009, June 2009) if they are present and then when you click on a month, you echo all news entries from that month, much like this - or possibly using a drop down box.

 

As I said I don't really know php so I don't know where to start, can anybody help me with the code or perhaps point me in the right direction?

 

Would be much appreciated, thanks for reading

Link to comment
Share on other sites

You can order database entries using SQL. In your SQL query just do something like:

 

SELECT datetime FROM table ORDER BY datetime DESC

 

Alternatively, you can use ASC instead of DESC to sort the results in ascending order.

Link to comment
Share on other sites

Sorry I dont' think I explained my problem very well,

 

I already have sorted them, well I sorted them by id which is an auto incrementing number which essentially does the same thing.

 

What I would like is sort of an archive webpage, where the php generates a list of months, and clicking on a month would take you to all posts from that month.

Link to comment
Share on other sites

SELECT * FROM table WHERE datetime BETWEEN date1 AND date2

That should get all the entries where datetime is between 2 dates. You'll have to fill in the 2 dates of course. I feel like you can use MySQL INTERVAL for this, but I never had great luck with using that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.