download201401 2016-08-13 18:55 采纳率: 0%
浏览 65
已采纳

如何用处理后端的php设计分页?

I am very new to web programming. I am making a website that basically displays a long list of items. However, the list is too long to fit in one single page, so I decided to use pagination (page 1,2,3... Next)

Currently, my php code is handling the backend such that it retrieves all data from the database and echo them back to the html frontend (jQuery ajax) which then displays it to the browser.

My question is should I retrieve all data when the site loads (at $(document).ready()) or should I load the data when user requests to see certain page? Which one is a better design? Lastly, any example on pagination backend design?

  • 写回答

1条回答 默认 最新

  • dongtaogu8510 2016-08-13 20:03
    关注

    Probably I wouldn't load all the data from the database at once, but only the data needed to fill a particular page, for performance reasons.

    On the backend side, you can do that with MySQL queries (I guess you would use MySQL) like:

    SELECT fields FROM tables WHERE condition
       LIMIT numItemsPerPage OFFSET pageStartPosition
    

    where the numItemsPerPage gives the number of itens per page and pageStartPosition gives the start position of the items at that page (= numItemsPerPage * (pageNumber - 1), assuming you start from page 1). Then you would sent this data to the page after an AJAX request for the data of page pageNumber.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!