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.

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大