pagination with a sorted array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
samusk
Forum Newbie
Posts: 1
Joined: Fri Nov 09, 2007 10:29 am

pagination with a sorted array

Post by samusk »

I am having a small problem with my pagination.

I create a sorted array with about 5 elements and they are sorted by closest distance. It works fine until I add pagination. For some reason the first page works great until I go to the next one. The algorithm to find the distance is messed up meaning the last element of distance on the previous page is not even related to the first element of distance on the next page.

I have tried a number of pagination including looking at ajax a few times.

My question is this. Instead of querying the database each time for each page, I want to create the array and sort it and use it instead of the mysql query. The problem then arises on how to code it to work with the array rather than the query.

The array is a 2d array by the way.

Any help would be greatly appreciated.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

If you can, do all sorting inside MySQL and use LIMITS for pagination - MySQL is WAAYYYYY faster at this sort of thing.

My instinct tells me that using the Google Maps API and generating lon/lat pairs is the probably the best solution - you can then order by a function of the change in lon/lat. Hint: Pythagoras was definitely on to something.

Also, some DB engines have geographic functions that might be a tad more DRY.

Failing that, there's always the $_SESSION array to cache pre-sorted results, but that's not necessarily the *best* solution.
Post Reply