Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Next query in Mysql

    Okay, I feel stupid simply because of the fact that I cannot get it to work. Im using PHP an what im trying to do its pull queries from the database. This is how I want it set up. I have posts, much like a forum post, except the page only displays one post at a time! I want to have a link to have that page pull up a whole nother post. I used to have it set up to where when you click the link it just grabbed the next ID, preset. I want it to do it automatically, because when there is a post that isnt appopriate, I delete it and then the ID's are all screwed up and not all there. Hopefully everyone can understand what Im saying. If you need further examples please just ask. I've been trying to do this for a year and I gave up after awhile.

    Thanks,
    Nate

  • #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    Code:
    select
    postid,
    foo,
    bar,
    qux,
    (select id from yourtable 
       where id = (select min(id) from yourtable
                          where id > dt.id
                       )
    ) as nextrow,
    (select id from yourtable 
       where id = (select max(id) from yourtable
                          where id < dt.id
                       )
    ) as previousrow
    from yourtable as dt
    this will give you next and previous posts irrespective of whether or not there are ids missing in your table.

  • #3
    New Coder
    Join Date
    Aug 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Im still not sure how i can get that to work! I want it to show one post, then have it link to another one... hmm.. not sure how to put it into a query either! Please help explain..

    thx,
    Nate

  • #4
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    i've already given you the query, you may need to adapt it to your column names.

    it will only show you three total items at a time, you would reference postid as your current row and reference nextrow and previous row as your row after and before that postid.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Hmm yeah Nate that query should have solved your problems... did you need help implamenting it into a server side language?

  • #6
    New Coder
    Join Date
    Aug 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, I need help using it with PHP... maybe Im just not looking at it the right way! Please help!!!

  • #7
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    I'll move the thread over to the php forum so you can get quicker help there.

    you should post the part of your code where you are trying to implement this so folks can see where you are having trouble.


  •  

    Posting Permissions

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