SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot New Oddity's Avatar
    Join Date
    Jun 2001
    Location
    Georgia
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pointer in result from MySQL Statement

    I have a page set up so that it only show a certain number of results per page. When the user goes to the second page, I want to show the next group of results. I tried a for loop to pull out the previous rows so that I could get to the next group, but that didn't work.
    --Odd
    "We all live in a yellow subroutine."
    "Some call it insanity; I call it inspiration!"

  2. #2
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use the MySQL Limit Feature.

    SELECT * FROM table_name, LIMIT 0,100

    This pulls out the first row all the way to the 100th

    LIMIT 100,200

    That would be the next page.

  3. #3
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use LIMIT in your SQL statement:

    SELECT * FROM table LIMIT 0,10 - displays first 10 results
    SELECT * FROM table LIMIT 10,10 - displays 10 results starting at number 10
    SELECT * FROM table LIMIT 20,10 - displays 10 results starting at number 20
    SELECT * FROM table LIMIT 30,10 - displays 10 results starting at number 30
    SELECT * FROM table LIMIT 40,10 - displays 10 results starting at number 40

    As you can see, the syntax is LIMIT $start, $number where $start is the result to start displaying at, and $number is how many to actually return.

    Now you can let the user provide a "page number" via a query string and then display those results:

    script.php?page=5

    $page is 5

    $number = 10;
    $start = ($page - 1) * $number;
    $sql = "SELECT * FROM table LIMIT $start, $number";

  4. #4
    SitePoint Zealot New Oddity's Avatar
    Join Date
    Jun 2001
    Location
    Georgia
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you all very much... Guess I gots to brush up on my SQL symantics...
    --Odd
    "We all live in a yellow subroutine."
    "Some call it insanity; I call it inspiration!"


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
  •