1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Simple pagination

Discussion in 'PHP' started by mnymkr, Jan 24, 2007.

  1. #1
    I have been teaching myself php and doing ok at it. At least I am happy with my progress.

    I have a hard time understanding how pagination works no matter how much reading I do.

    I was hoping some of you might spit some thoughts at me that helped you or might help me understand how to get my mind around the idea.

    Lets start with , I run a query and echo a list.....how might I paginate that?
     
    mnymkr, Jan 24, 2007 IP
  2. SilkySmooth

    SilkySmooth Well-Known Member

    Messages:
    1,583
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    180
    #2
    Hey,

    I wrote a guide for pagination a long time ago, it is geared towards novices so it should give you a good understanding. I cant post links as I am new so replace the DOT...

    http://www.devshed DOT com/c/a/PHP/Paginating-MySQL-Data-with-PHP/

    If you have any questions with it, come back and I will gladly help.
     
    SilkySmooth, Jan 24, 2007 IP
  3. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #3
    thanks i will check it out today!
     
    mnymkr, Jan 25, 2007 IP
  4. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #4
    OK that makes more sense. I would need to sit down and do it myself. One of the things I am dealing with is I am building a custom plugin for something in Joomla. There is an API I am using (below)

    I am trying to relate what I am seeing here to what I am reading in tutorials.

    1.9.4.1 Pagination
    Part of the list framework, CB API supports a powerful, yet simple, paging API using this
    function:
    /**
    * Writes the html links for pages inside tabs, eg, previous 1 2 3 ... x next
    * @param array: paging parameters. ['limitstart'] is the record number to start
    dislpaying from will be ignored
    * @param string postfix for identifying multiple pagings/search/sorts (optional)
    * @param int Number of rows to display per page
    * @param int Total number of rows
    * @param string cb task to link to (default: userProfile)
    * @return string html text displaying paging
    */
    function _writePaging($pagingParams, $postfix, $limit, $total,
    $task="userProfile")
     
    mnymkr, Jan 25, 2007 IP
  5. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Pagination at it's simplest is like this:

    
    define ('MAX_PAGES', 10);
    define ('ITEMS_PER_PAGE', 10);
    
    // validate input
    $page = intval (@$_GET['page']);
    $page = ($page > MAX_PAGES || $page < 1) ? 1 : $page;
    
    $result = mysql_query ('SELECT * FROM `table` WHERE 1 LIMIT '.($page - 1).', '.ITEMS_PER_PAGE);
    
    while ($item = mysql_fetch_assoc ($result)) {
        // output the items
    }
    
    // Pagination links
    if ($page > 1) {
        echo '<a href="?page='.($page-1).'">Prev page</a>';
    }
    if ($page < 10) {
    echo '<a href="?page='.($page+1).'">Next page</a>';
    }
    PHP:
     
    exam, Jan 25, 2007 IP
  6. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #6
    cool i will definitly try this out.

    as far as the code above if someone has written this into a function how might i use the said function?
     
    mnymkr, Jan 25, 2007 IP
  7. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #7
    exam, sorry but your query is wrong...

    It should be
    $result = mysql_query ('SELECT * FROM `table` WHERE 1 LIMIT '. ($page - 1) * ITEMS_PER_PAGE . ', ' . ITEMS_PER_PAGE);

    (otherwise you would start from 0 for first page, 1 for second page, etc, while you want to start from 0 for first page, then 10 (for example) for second page, etc...)

    ;)
     
    picouli, Jan 26, 2007 IP
    exam likes this.
  8. mani

    mani Peon

    Messages:
    679
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #8
    mani, Jan 26, 2007 IP
  9. SilkySmooth

    SilkySmooth Well-Known Member

    Messages:
    1,583
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    180
    #9
    mnymkr,

    It sounds like you already have an API with a paging function and your really just stuck with how to use that function. If thats the case, have you tried calling it to see what happens?

    To call the function you would just do:

    
    _writePaging($pagingParams,$postfix,20,1000,$task="userProfile");
    
    PHP:
     
    SilkySmooth, Jan 26, 2007 IP
  10. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #10
    i guess there are already some answers in this thread, but you need the number of results from your query, the number of results you want to display per page and the offset - after which result you want to start.

    for example you have 85 results and want to show 30 results on each page, then you need a function pagination($total_results, $offset, $length) and call it like pagination(85, 0, 30) for page 1, pagination(85, 30, 30) for page2 etc.
    and you modify your sql query a bit, just add "LIMIT $offset, $length" at the end. then you pass $lenght and $offset to your result page in your url.
    and you have to mark the current page somehow (bold for example) and figure out how much pages you have with 85 results and 30/per page, guess you need php floor() here, quite simple math, nothing magic :)
     
    falcondriver, Jan 26, 2007 IP
  11. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #11
    this is a dumb question but above with the function i posted , does it spit out the list or do I still need to do a for each row as rows { echo .....
     
    mnymkr, Jan 27, 2007 IP
  12. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #12
    Barti1987, Jan 27, 2007 IP
  13. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #13
    You're right. :( I just typed that in w/o testing or double checking. Sorry. :)
     
    exam, Jan 27, 2007 IP
    picouli likes this.
  14. Luke Jones

    Luke Jones Peon

    Messages:
    427
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Hello,
    How would I alter this code for a script that is not using a database?
    I'm using an api feed, and an xml feed.
    How do I alter the results= line?
     
    Luke Jones, Aug 29, 2007 IP