download201401
download201401
采纳率0%
2016-08-13 18:55

如何用处理后端的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 dongtaogu8510 5年前

    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.

    点赞 评论 复制链接分享

相关推荐