SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Limiting Results on Page

    Hey People, right i have all my other problems sorted now and ive managed to get things working, all that was messin it up was the me missing an echo at the begginning of one of the variables


    Anyways here i am now wanting to limit the number of results displayed upon a page and the when i click next it goes to the next lot.

    I have the next and previous links working and and its displays more info on the next page but i have a problem with this line:

    PHP Code:
    $StartRow = ($PageNum 1) * 4;
    $EndRow    = ($PageNum 4) - 1
    which works along side this in the sql query:

    PHP Code:
    .    ' LIMIT ' $StartRow .' , ' $EndRow 
    Heres the situation, the first page displays 3 when i want it to display 4 and the other pages display 7

    I think i know why, because $StartRow = ($PageNum - 1) * 4, on page 1 we are -1 from 1 which = 0 and then * by 4 ( 0 * 4 = 0) so the answer for the start of LIMIT would be 0 and then the $EndRow = ($PageNum * 4) - 1
    is * whatever page your on by 4 then - 1 so if i was on page 1 it would be 1*4 = 4, then 4-1=3 (thats why its displaying 3 on the first page cause the answer for the end of LIMIT is 3)

    page 2 would be 2*4=8, then 8-1=7 (and thats why its displaying 7 on the next page because the answer for LIMIT is returned as 7)

    Wow arent i clever, one problem though, Im not clever enough to be able to fix it

    Thats where you guys come in with your amazing tallent for getting me on the right track

    Thanks in advance
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  2. #2
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $range 4;
    $start = (($pageNum -1) * $range) + 1;

    $sql .= " LIMIT $start$range"
    I usually just go with offsets though, much easier:

    PHP Code:
    $start = isset($_GET['offset'])?$_GET['offset']:0;
    $range 4;

    $sql .= " LIMIT $start$range"

  3. #3
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    brilliant, we now have 4 on each page, but the problem is its the same 4

    Anything else i need to add??


    p.s it needs to start from the number it finished on from the last page

    i.e PAGE 1 = 0, 1, 2, 3
    PAGE 2 = 4, 5, 6, 7

    and so on....
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  4. #4
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    then you can do
    PHP Code:
    $start = isset($_GET['offset'])?$_GET['offset']:0
    $range 4;

    $sql .= " LIMIT $start$range"
    $next '<a href="page.php?offset=">'.($start+1).'</a>'
    is this what you want?


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
  •