How do I calculate the offsets for pagination?

前端 未结 4 609
日久生厌
日久生厌 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: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 'prev';
    

提交回复
热议问题