1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[Docs] Using Pagination

Discussion in 'Design and Layout' started by Nick, Jan 13, 2010.

  1. Nick

    Nick Well-Known Member

    There are two ways to paginate data in Hotaru.

    1. Only get the data you need for each page
    2. Get all the data first, then divide it into pages.

    The first is more memory efficient and should be used in most cases. Only use the second for small sets of data.

    Here are example for getting comments.

    1. Paginate ALL non-archived comments in the database

    PHP:
        // get the total number of records
        
    $count_sql "SELECT count(*) AS number FROM " TABLE_COMMENTS  " WHERE comment_archived = %s";
        
    $count $h->db->get_var($h->db->prepare($count_sql'N'));

        
    // get the prepared SQL query
        
    $sql "SELECT * FROM " TABLE_COMMENTS  " WHERE comment_archived = %s";
        
    $query $h->db->prepare($sql'N'); // N = No (not archived)

        // number of items per page
        
    $items_per_page 10;

        
    // name of database table to check for updates (cache refreshes when table is updated)
        
    $cache_table 'comments';

        
    // get results for this page
        
    $pagedResults $h->pagination($query$count$items_per_page$cache_table);
    Then loop through each item:

    PHP:
        if ($pagedResults->items) {
            foreach (
    $pagedResults->items as $comment) {
                
    // do stuff with this row of data
            
    }
        }
    Finally, display the page links at the bottom:

    PHP:
        echo $h->pageBar($pagedResults);
    2. Get all comments for a SINGLE POST, then divide them into pages.

    PHP:
        // get the data set
         
    $sql "SELECT * FROM " TABLE_COMMENTS  " WHERE comment_post_id = %d";
         
    $comments = %h->db->get_results($h->db->prepare($sql$post_id));

        
    // number of items per page
        
    $items_per_page 10;

        
    // get items for this page
        
    $pagedResults $h->paginationFull($comments$items_per_page);
    Then loop through each item:

    PHP:
        if ($pagedResults->items) {
            foreach (
    $pagedResults->items as $comment) {
                 
    // do stuff with this row of data
             
    }
        }
    Finally, display the page links at the bottom:

    PHP:
        echo $h->pageBar($pagedResults);
     
    Last edited: Feb 20, 2010

Share This Page