Jump to content


Photo

dynamic selection of content - ermm... help!


  • Please log in to reply
3 replies to this topic

#1 stuart.cole

stuart.cole
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 10 October 2006 - 03:51 PM

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 :(


#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 10 October 2006 - 03:56 PM

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
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 shiny_spoon

shiny_spoon
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationCanada

Posted 10 October 2006 - 04:05 PM

Like so:

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

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

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

...replacing
'news'
and
'date'
with whatever your columns are named. You might also want to read the suggested documentation to know how everything works. :)

#4 stuart.cole

stuart.cole
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 10 October 2006 - 04:11 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users