-2

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
  • 2nd is more appropriate to do as It won't affect more on your server while fetching you need to use limit for second approach. There are tons of tutorial on internet about it phppot.com/php/ajax-pagination-with-php Commented Oct 6, 2017 at 10:29

1 Answer 1

0

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), ...

Sign up to request clarification or add additional context in comments.

8 Comments

I don't want the page to refreshes this is why I thought i'd pass the request with ajax. I've taken the second approach, the only problem is now that if i search for something, it only filters out what's on the current page since I'm using only javascript to filter out the results. I guess I'd have to make the search functionality with php and and pass an ajax request so the page doesn't referesh ?
No problem with Ajax. Now your second question, explain that better, i do not get it. Your SELECT gets all the possible values, but you tell MySLQ to give you only the first 10 results. If you run another query, it does not even know about the limit you put on the first one.
my initial query only selects 10 records per page. now if I'm on page 1 and there is a record that I want to search for using my search input field, but the record is on the second page, how do I go about that ?
Why would you search something your user does not even see? When you do a search in Google, you can only click on the links it shows you. So your first search for fruits returns (apples, banana, grapes, kiwi). Why would a user of your site try now to click on result rasberry? It is not available in the results that are displayed. The user would have to move forward in the pages to find rasberry, then click on it. Explain further with more detais (of better yet, accept the answer here and start a new question with details).
yeah but if I have a lotta of records, the user would have to go through all the pages to find what he's looking for, why not make it easier for the user to just type in what he's looking for instead of going through the pages
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.