How do I calculate the offsets for pagination?

前端 未结 4 585
日久生厌
日久生厌 2020-12-15 00:46

I am working on a pagination feature in a web service I am writing, but my lack of math insight is killing me now. I have a couple of keys: totalItems, cu

相关标签:
4条回答
  • 2020-12-15 01:01

    If for limit you are referring to the count of items per page, then:

    currentItems: same as limit
    currentPage: floor(start / limit)
    totalPages: ceil(totalItems / limit)
    last: totalPages * limit
    previous: (currentPage-1 // Should be greater or equal to 0) * limit
    next: (currentPage+1 // Should be less or equal than totalPages) * limit
    

    I'ts just an approximation...

    0 讨论(0)
  • 2020-12-15 01:04

    If you work with mysql its

    LIMIT offset, items_per_page
    

    To calculate the offset u can use

    $offset = ($page - 1) * $items_per_page;
    

    Then replace the $page accordingly.

    Last

    $last_offset = ($totalPages - 1) * $items_per_page;
    

    Previous

    $previous_offset = (($currentPage - 1) - 1) * $items_per_page;
    

    Next

    $next_offset = (($currentPage + 1) - 1) * $items_per_page;
    

    EDIT :

    if ($previous_offset > 0) echo '<a href="?start='.$previous_offset.'&limit='.$items_per_page.'>prev</a>';
    
    
    if ($next_offset <= $totalPages * $items_per_page) echo '<a href="?start='.$next_offset.'&limit='.$items_per_page.'">prev</a>';
    
    0 讨论(0)
  • 2020-12-15 01:09

    For here I'm used code below and works fine.

    <?php
    //curpage = current page;
    //-1 = for adjust actual page;
    //30 = total post for each page;
    //5 = jump or offset determided for start count post 
    ?>
    
    <?php $offset = (($curpage - 1) * 30) + 5 ;?>
    <!-- print for view test -->
    <h1><?php echo $offset ?></h1>
    <?php
        $args_2 = array(
          'offset' => $offset,
          'post_type' => 'post',
          'posts_per_page' => 30,
          'category_name'     => $cat_filter,
          'post_status' => 'publish',
          'paged' => $paged
        );
    
      $query = new WP_Query( $args_2 );
    
      if ( $query->have_posts() ) :
        while ( $query->have_posts() ) : $query->the_post();
          ?>
    
    0 讨论(0)
  • 2020-12-15 01:19

    I have gone through lots of articles and create very simple formula for pagination

    offset = (limit * page no) - limit
    

    For example, if the limit is 5

    LIMIT 5
        page 1 
            offset : no (No need to use offset)
            (5 - 1) - 5
             select * from users limit 5
    
        page 2
            offset : 5
            (5 * 2) - 5
            select * from users limit 5 offset 5
    
        page 3 
            offset 10
            (5 * 3) - 5
            select * from users limit 5 offset 10
    
        page 4 
            offset 15
            (5 * 4) - 5
            select * from users limit 5 offset 15
    

    select * from table name limit 5 offset value (calculated from the formula)

    this is worked for me

    0 讨论(0)
提交回复
热议问题