Jump to content

Archived

This topic is now archived and is closed to further replies.

stuart.cole

dynamic selection of content - ermm... help!

Recommended Posts

Hi

I am building a news delivery site - with the news being delivered in 3 areas. The first should have the latest news item, the second area the second most recent item, and the third area, latest stories 3-7.

How can I get PHP to select the last item, next to last, and then 3-7 since the ID's of these items will change as new items are added all the time?

I know there is probably a very simple answer to this - but I've not found one yet :(

Share this post


Link to post
Share on other sites
Use the ORDER BY syntax in MySQL to get the stories ordered by date, and use the LIMIT syntax to restrict the number of requested rows.

You can find a link to the MySQL manual in my signature.

Regards
Huggie

Share this post


Link to post
Share on other sites
Like so:

Area 1: Latest
[code]<?php
$query = 'SELECT * FROM "news" ORDER BY "date" DESC LIMIT 0,1';
?>[/code]

Area 2: 2nd Latest
[code]<?php
$query = 'SELECT * FROM "news" ORDER BY "date" DESC LIMIT 1,1';
?>[/code]

Area 3: Results 3 to 7
[code]<?php
$query = 'SELECT * FROM "news" ORDER BY "date" DESC LIMIT 2,5';
?>[/code]

...replacing [code=php:0]'news'[/code] and [code=php:0]'date'[/code] with whatever your columns are named. You might also want to read the suggested documentation to know how everything works. :)

Share this post


Link to post
Share on other sites
Thanks both. It was the limit numbers that were confusing me - the rest was done but with your plain example I can see how that works now. Much appreciated.

Share this post


Link to post
Share on other sites

×

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.