Mysqli php ajax Pagination

2019-09-22 10:31发布

问题:

can someone please explain to me how to approach this issue?

I have a table with 4000 records, a select option and a search field to filter out the table

however I'm trying to figure out the best way to set up pagination.

1st Approach: Should I make one query against the database and populate the table with all records and then set up the pagination with javascript so I can be able to search for records that are not only shown on the current page?

2nd Approach Set up the pagination with PHP, make a query via ajax for each page? however I'm afraid that this approach would not allow me to filter out the table if I need to search for a record that is not on the current page.

回答1:

1st approach: definitely not, loading large number of results for nothing is not recommended.
2nd approach makes more sense. You can use the LIMIT statement inside your SELECT statement to decide which records you want. Ex. if you paginate on 10 elements perpage, you could do

 SELECT * FROM Table LIMIT 10

then

SELECT * FROM Table LIMIT 10,10

and so on. Indexes start at 0.

Note that you don't even need Ajax to do that, your next button can specify the offset for the next load of the page. It's a choice based on your knowledge, the size of the rest of the page (minimize load time), ...