+ Reply to Thread
Results 1 to 7 of 7

Thread: [MySQL] Getting 1st element of list

  1. #1
    Markstar is offline x10Hosting Member Markstar is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    45

    [MySQL] Getting 1st element of list

    Hi,
    I would like to allow visitors of my website to "turn the page" where there are n news items per page. Implementing the "Next " button/link is easy, but I struggle to get the "Previous" button/link to work.

    Here is an example with 3 items per page:
    news item # (nitem FROM news)
    ...
    16 // previous previous page
    15
    14
    13 // first item of previous page (13-11)
    12
    11
    10 // first item of current page (items 10 -
    9
    8
    7 // first item of next page (that's easy)
    6
    ...

    My problem was that nested queries didn't work with LIMITs and I would really like to get this with one SQL-statement and not have to go through a temporary result list in PHP.

    Thank you for your time!

  2. #2
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: [MySQL] Getting 1st element of list

    The aproach I currently use includes 2 MySQL queries: one to fetch the total count of entries and the second one to fetch the actual entries using an offset & limit. As for the actual code to generate the navigation, you can do some searches for pagination in php on google. The code I use is pretty big, so it'll take a lot of space to post it here.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  3. #3
    farscapeone's Avatar
    farscapeone is offline Community Advocate farscapeone is on a distinguished road
    Join Date
    Dec 2008
    Location
    Србија (Serbia)
    Posts
    1,166

    Re: [MySQL] Getting 1st element of list

    Try this: http://www.9lessons.info/2009/09/pag...l-and-php.html

    It's bad English but the code is OK

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: [MySQL] Getting 1st element of list

    Quote Originally Posted by Markstar View Post
    My problem was that nested queries didn't work with LIMITs
    What's your current query? Sub-selects can often be rewritten as joins.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    Markstar is offline x10Hosting Member Markstar is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    45

    Re: [MySQL] Getting 1st element of list

    Thanks guys, I have looked into pagination and while there is plenty of stuff out there, it is a little too much for what I'm trying to do. Additionally, most solutions require getting ALL news entries, which is exactly what I wanted to avoid. ;)

    @mission: Here is what I was thinking (but obviously doesn't work):
    Code:
    SELECT nitems FROM news WHERE nitem IN (SELECT nitms FROM news WHERE nitems > 10 ORDER BY nitems ASC LIMIT 3) ORDER BY DESC LIMIT 1;
    As I said, I know this doesn't work because MySQL doesn't support LIMIT in the nested query and also, ORDER BY is done at the very end.

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: [MySQL] Getting 1st element of list

    I assume "nitem" and "nitms" are misspellings of "nitems", and you're trying to get a specific range of rows ā la the head/tail strategy. As it turns out, neither subselects nor joins are needed. MySQL supports an extended LIMIT clause, where you can specify an offset before the row count:
    Code:
    SELECT nitems FROM news
        ORDER BY nitems ASC
        LIMIT :start, :length
    When asking about code, always include short, illustrative examples. It's part of being precise and informative.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  7. #7
    Markstar is offline x10Hosting Member Markstar is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    45

    Re: [MySQL] Getting 1st element of list

    Quote Originally Posted by misson View Post
    I assume "nitem" and "nitms" are misspellings of "nitems",
    Ups, sorry. I was writing this at the bus station, so I was a little bit in a hurry. Yes, but it's nitem. ;)

    and you're trying to get a specific range of rows ā la the head/tail strategy. As it turns out, neither subselects nor joins are needed. MySQL supports an extended LIMIT clause, where you can specify an offset before the row count:
    Code:
    SELECT nitems FROM news
        ORDER BY nitems ASC
        LIMIT :start, :length
    When asking about code, always include short, illustrative examples. It's part of being precise and informative.
    Hmm,
    Code:
    SELECT nitem FROM news
        ORDER BY nitem ASC
        LIMIT 10, 3
    Returns an empty result. :happysad:

    Here is what I currently have:
    Code:
    SELECT nitem
    FROM news
    WHERE cat =   'f'
    AND nitem >10
    ORDER  BY nitem ASC 
    LIMIT 3
    The output for this is 11, 12, 13 (but in practice could also be 12, 42, 134). Now I need to get the 13, so currently I go through the list in PHP until I reach the last element and save this as the variable that I use for the link to the previous page. :dunno:


    Edit: Ahh, I think I finally understood LIMIT x, y. If LIMIT get 2 values, the first one is the offset (not really the start).

    Since I don't really need to have a full list of all the pages (first, previous, next and last are enough), this will do nicely and I already have it up and running.

    Thank you!


    P.S.: But sadly it seems x10 is having some difficulties right now. Oh well, I hope it'll work again shortly. :cool:
    Last edited by Markstar; 01-29-2010 at 04:37 AM. Reason: Automerged Doublepost

+ Reply to Thread

Similar Threads

  1. List of only good Article Sites with PR
    By balaji2u in forum Advertising
    Replies: 6
    Last Post: 05-15-2010, 10:44 PM
  2. Please fix this code
    By like2program in forum Programming Help
    Replies: 5
    Last Post: 05-05-2008, 06:22 PM
  3. Dean's List.
    By Starshine in forum Off Topic
    Replies: 11
    Last Post: 02-13-2008, 11:37 AM
  4. XHTML help please
    By mac173 in forum Graphics & Webdesign
    Replies: 3
    Last Post: 01-29-2008, 11:39 AM
  5. Replies: 2
    Last Post: 12-23-2007, 05:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers