SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Going to previous and next record (not pagination)

    Hi all,

    I want to add links to a details page that will go to the very next, or previous, record in the db.

    All I've found are the pagination examples, but I don't see how that will work because the page has already called a specific ID/primary key and is not looking for a set number of results to be displayed.

    I just need to pull the previous and next id's out of the DB to put in the links.

    Any pointers on what I should be searching for in order to accomplish this would be grand.
    Thanks!
    No, I REALLY dislike having to use Joomla.

  2. #2
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hate to burst your bubble, but this is pagination, and you are looking for a set number of results - specifically, one.

    All pedantry aside, if you are defining your "Next" and "Previous" by where their primary keys fall numerically (i.e. the "next" record after 78 is 79), then it's quite simple.

    For next:
    Code:
    SELECT field1, field2, field2
    FROM table
    WHERE id > $id
    ORDER BY id ASC
    LIMIT 1
    I think you can see where previous then comes from:
    Code:
    SELECT field1, field2, field2
    FROM table
    WHERE id < $id
    ORDER BY id DESC
    LIMIT 1
    You'd choose which one to execute based on GET parameters; if it were me, I'd do something like
    www.example.com/page.php?id=78&action=next
    If $_GET['action'] is "next", then select the next record after $_GET['id']; if it is previous, select the previous record. If it's undefined, just select record $_GET['id'].
    PHP questions? RTFM
    MySQL questions? RTFM

  3. #3
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, thanks kromey.
    No, I REALLY dislike having to use Joomla.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •