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?
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
.