Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

only show 10 newest records 2

Status
Not open for further replies.

florens

IS-IT--Management
Nov 13, 2002
50
NL
Hi there,

I'm quite new to PHP, but I am trying to build a site which shows news items(which I insert myself). I store the newsitems in a database. I can extract the news items from the database but I only want the newest 10 to be shown on the site. The others should be archived in the database and remain accessible.

Does anyone know how to do this?
 
First you'll need to add the field ie dateline (int - 10) to the table. Then you need to give each news post a date/time when you insert them. I prefer to use unix timestamps simply put $thetime = time();

Then use that variable in the relative place of the insert statement ie:

mysql_query("INSERT INTO news_posts VALUES('', '$thetime' ...)");

Now to query the last 10 records you would put

$query = mysql_query("SELECT * FROM news_posts LIMIT 0, 10");

while($post = mysql_fetch_array($query)) {
// Content
}
 
Ooops I missed an important bit change:
$query = mysql_query("SELECT * FROM news_posts LIMIT 0, 10");

to:
$query = mysql_query("SELECT * FROM news_posts ORDER BY dateline DESC LIMIT 0, 10");
 
Thanks for your reply's.
I'm unable to test it right now, but I will test it asap and keep you posted.
 
It works like a charm, thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top