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?

Recommended Answers

All 3 Replies

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

This is the query I use on my CMS system:

$query="SELECT * FROM news ORDER BY id DESC LIMIT 5";

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.

commented: Helpful reply with good code example. +1

Ahh, that helps greatly. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.