du7999 2009-12-24 17:06 采纳率: 100%
浏览 53
已采纳

Prev链接的问题

Right now I have some links to move to a Previous page or to the Next page (or any page number).

ex. <--prev | 0 | 1 | 2 | 3 | next-->

My issues is that I am only listing items on the page with a "status='A'". (A for active... some items are no longer being listed on this page and would have a 'D' for Delete or 'S' for Sold Out).

In the SQL I have ... WHERE status='A' LIMIT $start_item, 20

My issues is with the Previous link. How should I calculate the $start_item for it? Right now I am using the current ID (the first ID on the page currently being displayed) and subtracting 20 from it (the number of items per page). But if there are any items that are not Active between the current ID and ID-20 then the 20 items being displayed when clicking on Prev will run into the current ID.

  • 写回答

2条回答 默认 最新

  • doujiao1984 2009-12-24 17:25
    关注

    Based on your example, it's a little odd to see the page numbering start at zero. If you use the following:

    start_item = (page_number - 1) * num_items_per_page
    

    ...you can display pages starting at 1.

    My issues is with the Previous link. How should I calculate the $start_item for it?

    From the sounds of things, you're passing the start_item in your request. I'd just use the page number, and calculate upon request. Then all you need in for your Prev and Next buttons is current_page_number - 1 and current_page_number + 1. It's also more accommodating if you decide to allow users to choose how many items per page.

    Keep in mind that LIMIT has two parameters:

    • starting row
    • offset

    ...so your limit would resemble this psuedocode:

    LIMIT [(page_number - 1) * num_items_per_page], 20
    

    if there are any items that are not Active between the current ID and ID-20 then the 20 items being displayed when clicking on Prev will run into the current ID.

    1. Because you have WHERE status = 'A', nonactive items (items whose status is not 'A') will never be returned from the query. That said...
    2. Item state will change - they can be deleted or sold as you say, which will cause the data to shift. It's always possible that the current item is on the cusp of the page, and another items state being changed could cause the current item to now be on the previous page.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题