how to set limit of numbers

Discussion in 'PHP' started by khan11, Nov 24, 2007.

  1. #1
    hello guys,

    i have lots of ids saved in mysql but i want to show them on different pages in division.

    For example:
    I have
    1
    2
    3
    4
    5
    6

    i want to show 3 on one page and the next three on other page.
    my query is:
    SELECT * FROM tablename ORDER BY id DESC LIMIT 3

    now i want to show first three on first page, and the other on next page.

    so how can i do that?

    quick help would be appreciated..
     
    khan11, Nov 24, 2007 IP
  2. live-cms_com

    live-cms_com Notable Member

    Messages:
    3,128
    Likes Received:
    112
    Best Answers:
    0
    Trophy Points:
    205
    Digital Goods:
    1
    #2
    I think if you do LIMIT 3,3 it has an offset of 3 and selects 3, so it would select IDs 4, 5 and 6. Then on the next page you would have LIMIT 6,3.
     
    live-cms_com, Nov 24, 2007 IP
    khan11 likes this.
  3. udkl_12_98

    udkl_12_98 Banned

    Messages:
    307
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    From the PHP script , POst a start number...
    For eg: if 3 displayed on first page ..... When user presses next , send number 4 ....

    xyz.com/index.php?startlimit=4

    Then start displaying from 4th.....

    then do the above
     
    udkl_12_98, Nov 24, 2007 IP
  4. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Basically you construct your Limit inside the query based on this formula:
    $limit = 'LIMIT ' .($pageno-1) * $rows_per_page .',' .$rows_per_page;

    So if you want to see the records from the second page your computed limit query would be:
    $limit = 'LIMIT ' .(2-1)*3,3; -> $limit = 'LIMIT 3,3';

    Search in Google for "mysql php pagination" for more detailed explanations and examples.
     
    hogan_h, Nov 24, 2007 IP
  5. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #5
    exactly this is what i wanted.

    thank you so much.. + goes for you.

    and for others,

    I really appreciate your quick reply. Thank you so much!!
     
    khan11, Nov 24, 2007 IP
  6. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #6
    yea, exactly this is my idea.. i already did this, only i didn't know of setting numbers. thank you!
     
    khan11, Nov 24, 2007 IP
  7. udkl_12_98

    udkl_12_98 Banned

    Messages:
    307
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No Problemo ....
     
    udkl_12_98, Nov 24, 2007 IP