954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Displaying News Articles

I've been thinking for awhile now that I should code a nice news system, and I have the structure all figured out, but what stumped me was: How do you display the articles with the latest first?

I would set up a MySQL table with columns Title, Date&Time, Body, ID, Author, Source (not in that order). How would I then go about retrieving them and listing them in reverse order?

Sp!ke
Light Poster
45 posts since Jul 2005
Reputation Points: 14
Solved Threads: 0
 

Use ORDER BY command in your database query.
If you not sure about SQL commands, do your search on google or yahoo.

zippee
Posting Whiz in Training
294 posts since Jan 2005
Reputation Points: 10
Solved Threads: 7
 

This is the query I use on my CMS system:
[PHP]$query="SELECT * FROM news ORDER BY id DESC LIMIT 5";[/PHP]

ORDER BY id DESC will get the latest article first (the article with the highest id).
LIMIT 5 is a useful thing - it gets only the amount of articles you specify. If you didn't have this, all your articles would show on one page, and if there are lots of posts, you will end up with a massive page.

alex_extreme
Light Poster
27 posts since Mar 2005
Reputation Points: 11
Solved Threads: 0
 

Ahh, that helps greatly. Thanks!

Sp!ke
Light Poster
45 posts since Jul 2005
Reputation Points: 14
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You