Jump to content

Getting Next Valid Row and Previous if exists


Pezmc

Recommended Posts

Trying to create the good old "previous" and "next" buttons in PHP for a database of news.

News is just taken from the database where the ID posted (get) is equal to the ID and published = 1 (whether its online)

 

Say I have this database

Id Title Published

1 News Item 1

3 News Item 1

6 News Item 0

7 News Item 1

9 News Item 1

 

How if the current id is 1 can I show only next and link to id 3?

or if the ID is 3 show previous (linking to one) and next linking to 7

or if the ID is 7 show previous (linking to three) and next linking to 9

or if 9 show just previous (linking to 7)

 

Sorry rather hard concept to explain!

 

Hope you can help!

Thanks! Got it to work with:

Query to get previous id : "SELECT id FROM TABLENAME WHERE id < $currentId order by id DESC LIMIT 1" (it will return 3)

Query to get next id : "SELECT id FROM TABLENAME WHERE id > $currentId order by id LIMIT 1" (it will return 6)

Archived

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

×
×
  • Create New...

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.