开发者

Sort database entries via a dropdown list

I'm curious if anyone could possibly help me, because I can't find anythi开发者_运维知识库ng exactly related to it anywhere, and it's driving me nuts.

I'd like to have a dropdown list on a page, that will give the visitor the option to sort all entries by year. I have entries from i.e. 2001, 2005, 2009, 2010. The years should be displayed in the dropdown, so the visitor can easily just select all entries dated 2001 if they want. The year for each entry is located in the one database table I have.

In other words, I simply want a "sort by" dropdown that you can see on pretty much any shopping site nowadays. But with set years.

Thanks in advance for any replies!


Well, you create a dropdown, make a query, and use the ORDER BY syntax to order it by a certain field.

So, to sort it by year (Oldest to Newest):

SELECT * FROM table ORDER BY Year ASC

Newest to Oldest

SELECT * FROM table ORDER BY Year DESC

Perhaps you mispoke, and meant to say filter by the year. In that case you would use the WHERE clause:

SELECT * FROM table WHERE Year="2010"

This would make it so that only posts with the year 2010 are included.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜