Calculating item offset for pagination

前端 未结 9 2140
眼角桃花
眼角桃花 2020-12-12 20:53

this seems like very simple maths but somehow, my brain cant think ...

i am trying to implement pagination and will need to calculate the item offset to use in limi

9条回答
  •  误落风尘
    2020-12-12 21:04

    A simple pagination equation that covers all scenerio will be:

    $page = $_GET['page'];
    
    $items_per_page = 20;  //for example
    
    $offset = 0;  //initial offset
    
    if ($page != 1) {
       $offset = ($page * $items_per_page) - $items_per_page;
    
    }
    

    Given items per page = 5

    OUTPUT

    when page = 1, offset = 0

    when page = 2, offset = 5

    when page = 3, offset = 10

    when page = 4, offset = 15 . . .

提交回复
热议问题